Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write .dart_tool/pub/workspace_ref.json #4240

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> writePackageConfigFile() async {
ensureDir(p.dirname(packageConfigPath));
writeTextFile(
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down