Skip to content

Handle relative git-url-paths correctly when --directory #2919

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

Merged
merged 8 commits into from
Mar 22, 2021
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
32 changes: 27 additions & 5 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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 'package:path/path.dart' as p;
import 'package:pub/src/source/path.dart';
import 'package:pub_semver/pub_semver.dart';
import 'package:yaml/yaml.dart';

Expand Down Expand Up @@ -311,25 +313,45 @@ class AddCommand extends PubCommand {
if (gitUrl == null) {
usageException('The `--git-url` is required for git dependencies.');
}
Uri parsed;
try {
parsed = Uri.parse(gitUrl);
} on FormatException catch (e) {
usageException('The --git-url must be a valid url: ${e.message}.');
}
final urlRelativeToEntrypoint = parsed.isAbsolute
? parsed.toString()
:
// Turn the relative url from current working directory into a relative
// url from the entrypoint.
p.url.relative(
p.url.join(Uri.file(p.absolute(p.current)).toString(),
parsed.toString()),
from: p.toUri(p.absolute(entrypoint.root.dir)).toString());

/// Process the git options to return the simplest representation to be
/// added to the pubspec.
if (gitRef == null && gitPath == null) {
git = gitUrl;
git = urlRelativeToEntrypoint;
} else {
git = {'url': gitUrl, 'ref': gitRef, 'path': gitPath};
git = {'url': urlRelativeToEntrypoint, 'ref': gitRef, 'path': gitPath};
git.removeWhere((key, value) => value == null);
}

packageRange = cache.sources['git']
.parseRef(packageName, git)
.parseRef(packageName, git, containingPath: entrypoint.pubspecPath)
.withConstraint(constraint ?? VersionConstraint.any);
pubspecInformation = {'git': git};
} else if (path != null) {
final relativeToEntryPoint = p.isRelative(path)
? PathSource.relativePathWithPosixSeparators(
p.relative(path, from: entrypoint.root.dir))
: path;
packageRange = cache.sources['path']
.parseRef(packageName, path, containingPath: entrypoint.pubspecPath)
.parseRef(packageName, relativeToEntryPoint,
containingPath: entrypoint.pubspecPath)
.withConstraint(constraint ?? VersionConstraint.any);
pubspecInformation = {'path': path};
pubspecInformation = {'path': relativeToEntryPoint};
} else if (sdk != null) {
packageRange = cache.sources['sdk']
.parseRef(packageName, sdk)
Expand Down
12 changes: 9 additions & 3 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ class GlobalPackages {
// dependencies. Their executables shouldn't be cached, and there should
// be a mechanism for redoing dependency resolution if a path pubspec has
// changed (see also issue 20499).
PackageRef ref;
try {
ref = cache.git.source.parseRef(name, {'url': repo}, containingPath: '.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we're going to write the pubspec.yaml somewhere other than ., so the containingPath should be something like $PUB_CACHE/global_packages/<package>/, shouldn't it?

I'm not sure it matters here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't write it to disk (yet), so it should not matter...

} on FormatException catch (e) {
throw ApplicationException(e.message);
}
await _installInCache(
cache.git.source
.refFor(name, repo)
ref
.withConstraint(VersionConstraint.any)
.withFeatures(features ?? const {}),
executables,
Expand Down Expand Up @@ -282,7 +287,8 @@ To recompile executables, first run `global deactivate ${dep.name}`.
var oldPath = p.join(_directory, '$package.lock');
if (fileExists(oldPath)) deleteEntry(oldPath);

writeTextFile(_getLockFilePath(package), lockFile.serialize(cache.rootDir));
writeTextFile(_getLockFilePath(package),
lockFile.serialize(p.join(_directory, package)));
}

/// Shows the user the currently active package with [name], if any.
Expand Down
Loading