From a6e3b0a0c4e47a650686d4ddb3569110176f6ce7 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Thu, 25 Apr 2024 13:00:06 +0000 Subject: [PATCH] Write .dart_tool/pub/workspace_ref.json --- lib/src/entrypoint.dart | 22 +++++++++++++++++++++- test/workspace_test.dart | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 704264d46..94f8dff6b 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 db1056d99..8ca27b964 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(