From 05f750c61543e6374ab73a84712808f866b40760 Mon Sep 17 00:00:00 2001 From: davidmorgan Date: Fri, 12 Dec 2025 16:43:04 +0100 Subject: [PATCH 1/2] Prepare `code_builder` for `fixnum` 1.2.0. --- pkgs/code_builder/CHANGELOG.md | 7 ++++++- pkgs/code_builder/lib/src/allocator.dart | 19 +++++++++++++++++-- pkgs/code_builder/pubspec.yaml | 2 +- pkgs/code_builder/test/allocator_test.dart | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkgs/code_builder/CHANGELOG.md b/pkgs/code_builder/CHANGELOG.md index fa40c8c84..bff03e2bd 100644 --- a/pkgs/code_builder/CHANGELOG.md +++ b/pkgs/code_builder/CHANGELOG.md @@ -1,4 +1,9 @@ -## 4.11.1-wip +## 4.11.1 + +* Convert imports of implementation libraries under `package:fixnum/src/*` into + imports of `package:fixnum/fixnum.dart` to prevent issues when version 1.2.0 + is released with platform-specific implementation libraries that generated + code should not import directly. ## 4.11.0 diff --git a/pkgs/code_builder/lib/src/allocator.dart b/pkgs/code_builder/lib/src/allocator.dart index 2e732b361..efb33c731 100644 --- a/pkgs/code_builder/lib/src/allocator.dart +++ b/pkgs/code_builder/lib/src/allocator.dart @@ -42,6 +42,11 @@ abstract class Allocator { String allocate(Reference reference); /// All imports that have so far been added implicitly via [allocate]. + /// + /// As a special case, imports of `package:fixnum/src/*` are changed to refer + /// to the public `package:fixnum/fixnum.dart`. Version 1.2.0 of the package + /// introduces platform-specific implementation libraries that must not be + /// imported directly. Iterable get imports; } @@ -50,8 +55,9 @@ class _Allocator implements Allocator { @override String allocate(Reference reference) { - final url = reference.url; + var url = reference.url; if (url != null) { + url = _fixUrl(url); _imports.add(url); } return reference.symbol!; @@ -80,10 +86,11 @@ class _PrefixedAllocator implements Allocator { @override String allocate(Reference reference) { final symbol = reference.symbol; - final url = reference.url; + var url = reference.url; if (url == null || _doNotPrefix.contains(url)) { return symbol!; } + url = _fixUrl(url); return '_i${_imports.putIfAbsent(url, _nextKey)}.$symbol'; } @@ -93,3 +100,11 @@ class _PrefixedAllocator implements Allocator { Iterable get imports => _imports.keys.map((u) => Directive.import(u, as: '_i${_imports[u]}')); } + +/// Applies hardcoded fixes to [url]. +String _fixUrl(String url) { + if (url.startsWith('package:fixnum/src/')) { + return 'package:fixnum/fixnum.dart'; + } + return url; +} diff --git a/pkgs/code_builder/pubspec.yaml b/pkgs/code_builder/pubspec.yaml index d551bcbf7..b1e184146 100644 --- a/pkgs/code_builder/pubspec.yaml +++ b/pkgs/code_builder/pubspec.yaml @@ -1,5 +1,5 @@ name: code_builder -version: 4.11.1-wip +version: 4.11.1 description: A fluent, builder-based library for generating valid Dart code. repository: https://github.com/dart-lang/tools/tree/main/pkgs/code_builder issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Acode_builder diff --git a/pkgs/code_builder/test/allocator_test.dart b/pkgs/code_builder/test/allocator_test.dart index 71c93cbf2..7e089f6b5 100644 --- a/pkgs/code_builder/test/allocator_test.dart +++ b/pkgs/code_builder/test/allocator_test.dart @@ -30,6 +30,15 @@ void main() { ]); }); + test('.simple replaces fixnum internal URIs', () { + allocator = + Allocator() + ..allocate(refer('Int64', 'package:fixnum/src/int64_native.dart')); + expect(allocator.imports.map((d) => d.url), [ + 'package:fixnum/fixnum.dart', + ]); + }); + test('.none should do nothing', () { allocator = Allocator.none; expect(allocator.allocate(refer('Foo', 'package:foo')), 'Foo'); @@ -47,5 +56,14 @@ void main() { 'dart:collection as _i1', ]); }); + + test('.simplePrefixing replaces fixnum internal URIs', () { + allocator = + Allocator.simplePrefixing() + ..allocate(refer('Int64', 'package:fixnum/src/int64_native.dart')); + expect(allocator.imports.map((d) => d.url), [ + 'package:fixnum/fixnum.dart', + ]); + }); }); } From 24e813fc76261bc3d99acbc819981f8ad28c1695 Mon Sep 17 00:00:00 2001 From: davidmorgan Date: Wed, 17 Dec 2025 09:24:22 +0100 Subject: [PATCH 2/2] Address review comments. --- pkgs/code_builder/CHANGELOG.md | 3 ++- pkgs/code_builder/lib/src/allocator.dart | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkgs/code_builder/CHANGELOG.md b/pkgs/code_builder/CHANGELOG.md index bff03e2bd..e153bd5e3 100644 --- a/pkgs/code_builder/CHANGELOG.md +++ b/pkgs/code_builder/CHANGELOG.md @@ -3,7 +3,8 @@ * Convert imports of implementation libraries under `package:fixnum/src/*` into imports of `package:fixnum/fixnum.dart` to prevent issues when version 1.2.0 is released with platform-specific implementation libraries that generated - code should not import directly. + code should not import directly. This is a temporary workaround, it will be + replaced by a new way of managing imports in a future major version release. ## 4.11.0 diff --git a/pkgs/code_builder/lib/src/allocator.dart b/pkgs/code_builder/lib/src/allocator.dart index efb33c731..c7e821b76 100644 --- a/pkgs/code_builder/lib/src/allocator.dart +++ b/pkgs/code_builder/lib/src/allocator.dart @@ -102,6 +102,8 @@ class _PrefixedAllocator implements Allocator { } /// Applies hardcoded fixes to [url]. +/// +/// See [Allocator.imports] for explanations. String _fixUrl(String url) { if (url.startsWith('package:fixnum/src/')) { return 'package:fixnum/fixnum.dart';