Skip to content

Commit 86bf8b2

Browse files
authored
Handle relative git-url-paths correctly when --directory (#2919)
1 parent 3716a68 commit 86bf8b2

File tree

8 files changed

+298
-100
lines changed

8 files changed

+298
-100
lines changed

lib/src/command/add.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:path/path.dart' as p;
6+
import 'package:pub/src/source/path.dart';
57
import 'package:pub_semver/pub_semver.dart';
68
import 'package:yaml/yaml.dart';
79

@@ -313,25 +315,45 @@ class AddCommand extends PubCommand {
313315
if (gitUrl == null) {
314316
usageException('The `--git-url` is required for git dependencies.');
315317
}
318+
Uri parsed;
319+
try {
320+
parsed = Uri.parse(gitUrl);
321+
} on FormatException catch (e) {
322+
usageException('The --git-url must be a valid url: ${e.message}.');
323+
}
324+
final urlRelativeToEntrypoint = parsed.isAbsolute
325+
? parsed.toString()
326+
:
327+
// Turn the relative url from current working directory into a relative
328+
// url from the entrypoint.
329+
p.url.relative(
330+
p.url.join(Uri.file(p.absolute(p.current)).toString(),
331+
parsed.toString()),
332+
from: p.toUri(p.absolute(entrypoint.root.dir)).toString());
316333

317334
/// Process the git options to return the simplest representation to be
318335
/// added to the pubspec.
319336
if (gitRef == null && gitPath == null) {
320-
git = gitUrl;
337+
git = urlRelativeToEntrypoint;
321338
} else {
322-
git = {'url': gitUrl, 'ref': gitRef, 'path': gitPath};
339+
git = {'url': urlRelativeToEntrypoint, 'ref': gitRef, 'path': gitPath};
323340
git.removeWhere((key, value) => value == null);
324341
}
325342

326343
packageRange = cache.sources['git']
327-
.parseRef(packageName, git)
344+
.parseRef(packageName, git, containingPath: entrypoint.pubspecPath)
328345
.withConstraint(constraint ?? VersionConstraint.any);
329346
pubspecInformation = {'git': git};
330347
} else if (path != null) {
348+
final relativeToEntryPoint = p.isRelative(path)
349+
? PathSource.relativePathWithPosixSeparators(
350+
p.relative(path, from: entrypoint.root.dir))
351+
: path;
331352
packageRange = cache.sources['path']
332-
.parseRef(packageName, path, containingPath: entrypoint.pubspecPath)
353+
.parseRef(packageName, relativeToEntryPoint,
354+
containingPath: entrypoint.pubspecPath)
333355
.withConstraint(constraint ?? VersionConstraint.any);
334-
pubspecInformation = {'path': path};
356+
pubspecInformation = {'path': relativeToEntryPoint};
335357
} else if (sdk != null) {
336358
packageRange = cache.sources['sdk']
337359
.parseRef(packageName, sdk)

lib/src/global_packages.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,14 @@ class GlobalPackages {
9090
// dependencies. Their executables shouldn't be cached, and there should
9191
// be a mechanism for redoing dependency resolution if a path pubspec has
9292
// changed (see also issue 20499).
93+
PackageRef ref;
94+
try {
95+
ref = cache.git.source.parseRef(name, {'url': repo}, containingPath: '.');
96+
} on FormatException catch (e) {
97+
throw ApplicationException(e.message);
98+
}
9399
await _installInCache(
94-
cache.git.source
95-
.refFor(name, repo)
100+
ref
96101
.withConstraint(VersionConstraint.any)
97102
.withFeatures(features ?? const {}),
98103
executables,
@@ -282,7 +287,8 @@ To recompile executables, first run `global deactivate ${dep.name}`.
282287
var oldPath = p.join(_directory, '$package.lock');
283288
if (fileExists(oldPath)) deleteEntry(oldPath);
284289

285-
writeTextFile(_getLockFilePath(package), lockFile.serialize(cache.rootDir));
290+
writeTextFile(_getLockFilePath(package),
291+
lockFile.serialize(p.join(_directory, package)));
286292
}
287293

288294
/// Shows the user the currently active package with [name], if any.

0 commit comments

Comments
 (0)