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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reapply binstub fix #4258

Merged
merged 2 commits into from
May 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,15 @@ To recompile executables, first run `$topLevelProgram pub global deactivate $nam
/// Returns an [Entrypoint] loaded with the active package if found.
Future<Entrypoint> find(String name) async {
final lockFilePath = _getLockFilePath(name);
late LockFile lockFile;
late final LockFile lockFile;
try {
lockFile = LockFile.load(lockFilePath, cache.sources);
} on IOException {
// If we couldn't read the lock file, it's not activated.
dataError('No active package ${log.bold(name)}.');
}

// Remove the package itself from the lockfile. We put it in there so we
// could find and load the [Package] object, but normally an entrypoint
// doesn't expect to be in its own lockfile.
final id = lockFile.packages[name]!;
lockFile = lockFile.removePackage(name);

Entrypoint entrypoint;
if (id.source is CachedSource) {
Expand Down Expand Up @@ -623,12 +619,12 @@ try:
binStubScript ==
p.basenameWithoutExtension(executable.relativePath)) {
log.fine('Replacing old binstub $file');
deleteEntry(file);
_createBinStub(
entrypoint.workspaceRoot,
activatedPackage(entrypoint),
p.basenameWithoutExtension(file),
binStubScript,
overwrite: true,
isRefreshingBinstub: true,
snapshot:
executable.pathOfGlobalSnapshot(entrypoint.workspaceRoot.dir),
);
Expand Down Expand Up @@ -685,6 +681,7 @@ try:
executable,
script,
overwrite: overwriteBinStubs,
isRefreshingBinstub: false,
snapshot: entrypoint.pathOfSnapshot(
exec.Executable.adaptProgramName(package.name, script),
),
Expand Down Expand Up @@ -767,14 +764,13 @@ try:
String script, {
required bool overwrite,
required String snapshot,
required bool isRefreshingBinstub,
}) {
var binStubPath = p.join(_binStubDir, executable);
if (Platform.isWindows) binStubPath += '.bat';

// See if the binstub already exists. If so, it's for another package
// since we already deleted all of this package's binstubs.
String? previousPackage;
if (fileExists(binStubPath)) {
if (!isRefreshingBinstub && fileExists(binStubPath)) {
final contents = readTextFile(binStubPath);
previousPackage = _binStubProperty(contents, 'Package');
if (previousPackage == null) {
Expand Down Expand Up @@ -850,7 +846,7 @@ fi
// it into place afterwards to avoid races.
final tempDir = cache.createTempDir();
try {
final tmpPath = p.join(tempDir, binStubPath);
final tmpPath = p.join(tempDir, p.basename(binStubPath));

// Write this as the system encoding since the system is going to
// execute it and it might contain non-ASCII characters in the
Expand Down Expand Up @@ -948,3 +944,17 @@ fi
return match == null ? null : match[1];
}
}

/// The package that was activated.
///
/// * For path packages this is [Entrypoint.workspaceRoot].
/// * For cached packages this is the sole dependency of
/// [Entrypoint.workspaceRoot].
Package activatedPackage(Entrypoint entrypoint) {
if (entrypoint.isCachedGlobal) {
final dep = entrypoint.workspaceRoot.dependencies.keys.single;
return entrypoint.cache.load(entrypoint.lockFile.packages[dep]!);
} else {
return entrypoint.workspaceRoot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
// 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:io';

import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import '../../descriptor.dart' as d;
Expand Down Expand Up @@ -32,4 +35,60 @@ void main() {
]),
]).validate();
});

test(
'the binstubs of hosted package runs pub global run if there is no snapshot',
() async {
final server = await servePackages();
server.serve(
'foo',
'1.0.0',
contents: [
d.dir('bin', [d.file('script.dart', "main() => print('ok');")]),
],
pubspec: {
'name': 'foo',
'executables': {'foo-script': 'script'},
},
);

await runPub(
args: ['global', 'activate', 'foo'],
output: contains('Installed executable foo-script.'),
);

await d.dir(cachePath, [
d.dir('bin', [
d.file(
binStubName('foo-script'),
contains('global run foo:script'),
),
]),
]).validate();

// Force refresh of snapshot/binstub
Directory(
p.join(d.sandbox, cachePath, 'global_packages', 'foo', 'bin'),
).deleteSync(recursive: true);
final binstub = p.join(
d.sandbox,
cachePath,
'bin',
'foo-script${Platform.isWindows ? '.bat' : ''}',
);
final result =
await Process.run(binstub, [], environment: getPubTestEnvironment());
expect(result.stderr, '');
expect(result.exitCode, 0);
expect(result.stdout, contains('ok'));

await d.dir(cachePath, [
d.dir('bin', [
d.file(
binStubName('foo-script'),
contains('global run foo:script'),
),
]),
]).validate();
});
}