diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 704264d461..94f8dff6b9 100644 --- a/lib/src/entrypoint.dart +++ b/lib/src/entrypoint.dart @@ -407,7 +407,12 @@ See $workspacesDocUrl for more information.''', Entrypoint? _example; - /// Writes the .dart_tool/package_config.json file + /// Writes the .dart_tool/package_config.json file and workspace references to + /// it. + /// + /// If the workspace is non-trivial: For each package in the workspace write: + /// `.dart_tool/pub/workspace_ref.json` with a pointer to the workspace root + /// package dir. Future writePackageConfigFile() async { ensureDir(p.dirname(packageConfigPath)); writeTextFile( @@ -418,6 +423,21 @@ See $workspacesDocUrl for more information.''', .pubspec.sdkConstraints[sdk.identifier]?.effectiveConstraint, ), ); + if (workspaceRoot.workspaceChildren.isNotEmpty) { + for (final package in workspaceRoot.transitiveWorkspace) { + final workspaceRefDir = p.join(package.dir, '.dart_tool', 'pub'); + final workspaceRefPath = p.join(workspaceRefDir, 'workspace_ref.json'); + ensureDir(workspaceRefDir); + final relativeRootPath = + p.relative(workspaceRoot.dir, from: workspaceRefDir); + writeTextFile( + workspaceRefPath, + '${JsonEncoder.withIndent(' ').convert({ + 'workspaceRoot': relativeRootPath, + })}\n', + ); + } + } } /// Returns the contents of the `.dart_tool/package_config` file generated diff --git a/test/workspace_test.dart b/test/workspace_test.dart index db1056d991..8ca27b964b 100644 --- a/test/workspace_test.dart +++ b/test/workspace_test.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'dart:convert'; import 'dart:io'; import 'package:path/path.dart' as p; @@ -51,6 +52,25 @@ void main() { ], generatorVersion: '3.5.0', ).validate(); + final workspaceRefA = jsonDecode( + File( + p.join( + sandbox, + appPath, + 'pkgs', + 'a', + '.dart_tool', + 'pub', + 'workspace_ref.json', + ), + ).readAsStringSync(), + ); + expect(workspaceRefA, {'workspaceRoot': p.join('..', '..', '..', '..')}); + final workspaceRefMyApp = jsonDecode( + File(p.join(sandbox, appPath, '.dart_tool', 'pub', 'workspace_ref.json')) + .readAsStringSync(), + ); + expect(workspaceRefMyApp, {'workspaceRoot': p.join('..', '..')}); }); test(