Skip to content

Commit 53a69e2

Browse files
authored
Fix .packages entries of relative path deps when using --directory (#2916)
1 parent d6308ef commit 53a69e2

16 files changed

+253
-128
lines changed

lib/src/entrypoint.dart

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,18 @@ class Entrypoint {
186186

187187
/// Writes .packages and .dart_tool/package_config.json
188188
Future<void> writePackagesFiles() async {
189-
writeTextFile(packagesFile, lockFile.packagesFile(cache, root.name));
189+
writeTextFile(
190+
packagesFile,
191+
lockFile.packagesFile(cache,
192+
entrypoint: root.name, relativeFrom: root.dir));
190193
ensureDir(p.dirname(packageConfigFile));
191194
writeTextFile(
192195
packageConfigFile,
193-
await lockFile.packageConfigFile(
194-
cache,
195-
entrypoint: root.name,
196-
entrypointSdkConstraint: root.pubspec.sdkConstraints[sdk.identifier],
197-
));
196+
await lockFile.packageConfigFile(cache,
197+
entrypoint: root.name,
198+
entrypointSdkConstraint:
199+
root.pubspec.sdkConstraints[sdk.identifier],
200+
relativeFrom: root.dir));
198201
}
199202

200203
/// Gets all dependencies of the [root] package.
@@ -549,7 +552,8 @@ class Entrypoint {
549552
// If we can't load the pubspec, the user needs to re-run "pub get".
550553
}
551554

552-
dataError('${p.join(source.getDirectory(id), 'pubspec.yaml')} has '
555+
dataError(
556+
'${p.join(source.getDirectory(id, relativeFrom: '.'), 'pubspec.yaml')} has '
553557
'changed since the pubspec.lock file was generated, please run "pub '
554558
'get" again.');
555559
}
@@ -576,7 +580,7 @@ class Entrypoint {
576580
if (source is! CachedSource) return true;
577581

578582
// Get the directory.
579-
var dir = source.getDirectory(package);
583+
var dir = source.getDirectory(package, relativeFrom: '.');
580584
// See if the directory is there and looks like a package.
581585
return dirExists(dir) && fileExists(p.join(dir, 'pubspec.yaml'));
582586
});
@@ -616,7 +620,9 @@ class Entrypoint {
616620
}
617621

618622
final source = cache.source(lockFileId.source);
619-
final lockFilePackagePath = root.path(source.getDirectory(lockFileId));
623+
final lockFilePackagePath = root.path(
624+
source.getDirectory(lockFileId, relativeFrom: root.dir),
625+
);
620626

621627
// Make sure that the packagePath agrees with the lock file about the
622628
// path to the package.
@@ -765,7 +771,8 @@ class Entrypoint {
765771
cache.load(id).pubspec.sdkConstraints[sdk.identifier],
766772
);
767773
if (pkg.languageVersion != languageVersion) {
768-
dataError('${p.join(source.getDirectory(id), 'pubspec.yaml')} has '
774+
dataError(
775+
'${p.join(source.getDirectory(id, relativeFrom: '.'), 'pubspec.yaml')} has '
769776
'changed since the pubspec.lock file was generated, please run '
770777
'"pub get" again.');
771778
}

lib/src/global_packages.dart

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,14 @@ To recompile executables, first run `global deactivate ${dep.name}`.
236236
// Load the package graph from [result] so we don't need to re-parse all
237237
// the pubspecs.
238238
final entrypoint = Entrypoint.global(
239-
Package(result.pubspecs[dep.name],
240-
cache.source(dep.source).getDirectory(id)),
241-
lockFile,
242-
cache,
243-
solveResult: result);
239+
Package(
240+
result.pubspecs[dep.name],
241+
(cache.source(dep.source) as CachedSource).getDirectoryInCache(id),
242+
),
243+
lockFile,
244+
cache,
245+
solveResult: result,
246+
);
244247
if (!sameVersions) {
245248
// Only precompile binaries if we have a new resolution.
246249
await entrypoint.precompileExecutables();
@@ -261,10 +264,12 @@ To recompile executables, first run `global deactivate ${dep.name}`.
261264
// TODO(sigurdm): Use [Entrypoint.writePackagesFiles] instead.
262265
final packagesFilePath = _getPackagesFilePath(package);
263266
final packageConfigFilePath = _getPackageConfigFilePath(package);
264-
writeTextFile(packagesFilePath, lockFile.packagesFile(cache));
265-
ensureDir(p.dirname(packageConfigFilePath));
267+
final dir = p.dirname(packagesFilePath);
266268
writeTextFile(
267-
packageConfigFilePath, await lockFile.packageConfigFile(cache));
269+
packagesFilePath, lockFile.packagesFile(cache, relativeFrom: dir));
270+
ensureDir(p.dirname(packageConfigFilePath));
271+
writeTextFile(packageConfigFilePath,
272+
await lockFile.packageConfigFile(cache, relativeFrom: dir));
268273
}
269274

270275
/// Finishes activating package [package] by saving [lockFile] in the cache.
@@ -355,7 +360,7 @@ To recompile executables, first run `global deactivate ${dep.name}`.
355360
if (source is CachedSource) {
356361
// For cached sources, the package itself is in the cache and the
357362
// lockfile is the one we just loaded.
358-
entrypoint = Entrypoint.global(cache.load(id), lockFile, cache);
363+
entrypoint = Entrypoint.global(cache.loadCached(id), lockFile, cache);
359364
} else {
360365
// For uncached sources (i.e. path), the ID just points to the real
361366
// directory for the package.
@@ -515,8 +520,13 @@ To recompile executables, first run `global deactivate ${dep.name}`.
515520
await _writePackageConfigFiles(id.name, entrypoint.lockFile);
516521
await entrypoint.precompileExecutables();
517522
var packageExecutables = executables.remove(id.name) ?? [];
518-
_updateBinStubs(entrypoint, cache.load(id), packageExecutables,
519-
overwriteBinStubs: true, suggestIfNotOnPath: false);
523+
_updateBinStubs(
524+
entrypoint,
525+
cache.load(id),
526+
packageExecutables,
527+
overwriteBinStubs: true,
528+
suggestIfNotOnPath: false,
529+
);
520530
successes.add(id.name);
521531
} catch (error, stackTrace) {
522532
var message = 'Failed to reactivate '

lib/src/lock_file.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:collection';
66
import 'dart:convert';
77

88
import 'package:collection/collection.dart' hide mapMap;
9+
import 'package:meta/meta.dart';
910
import 'package:path/path.dart' as p;
1011
import 'package:pub_semver/pub_semver.dart';
1112
import 'package:source_span/source_span.dart';
@@ -218,7 +219,11 @@ class LockFile {
218219
///
219220
/// If [entrypoint] is passed, a relative entry is added for its "lib/"
220221
/// directory.
221-
String packagesFile(SystemCache cache, [String entrypoint]) {
222+
String packagesFile(
223+
SystemCache cache, {
224+
String entrypoint,
225+
@required String relativeFrom,
226+
}) {
222227
var header = '''
223228
This file is deprecated. Tools should instead consume
224229
`.dart_tools/package_config.json`.
@@ -231,7 +236,8 @@ Generated by pub on ${DateTime.now()}.''';
231236
Map<String, Uri>.fromIterable(ordered(packages.keys), value: (name) {
232237
var id = packages[name];
233238
var source = cache.source(id.source);
234-
return p.toUri(p.join(source.getDirectory(id), 'lib'));
239+
return p.toUri(
240+
p.join(source.getDirectory(id, relativeFrom: relativeFrom), 'lib'));
235241
});
236242

237243
if (entrypoint != null) map[entrypoint] = Uri.parse('lib/');
@@ -254,12 +260,13 @@ Generated by pub on ${DateTime.now()}.''';
254260
SystemCache cache, {
255261
String entrypoint,
256262
VersionConstraint entrypointSdkConstraint,
263+
@required String relativeFrom,
257264
}) async {
258265
final entries = <PackageConfigEntry>[];
259266
for (final name in ordered(packages.keys)) {
260267
final id = packages[name];
261268
final source = cache.source(id.source);
262-
final rootPath = source.getDirectory(id);
269+
final rootPath = source.getDirectory(id, relativeFrom: relativeFrom);
263270
Uri rootUri;
264271
if (p.isRelative(rootPath)) {
265272
// Relative paths are relative to the root project, we want them

lib/src/null_safety_analysis.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ class NullSafetyAnalysis {
118118
return nullSafetyComplianceOfPackages(
119119
result.packages.where((id) => id.name != fakeRootName),
120120
Package(rootPubspec,
121-
packageId.source.bind(_systemCache).getDirectory(packageId)));
121+
packageId.source.bind(_systemCache).getDirectory(packageId)),
122+
containingPath);
122123
}
123124

124125
/// Decides if all dependendencies (transitively) have a language version
@@ -133,7 +134,10 @@ class NullSafetyAnalysis {
133134
///
134135
/// Assumes the root package is opted in.
135136
Future<NullSafetyAnalysisResult> nullSafetyComplianceOfPackages(
136-
Iterable<PackageId> packages, Package rootPackage) async {
137+
Iterable<PackageId> packages,
138+
Package rootPackage,
139+
String containingPath,
140+
) async {
137141
NullSafetyAnalysisResult firstBadPackage;
138142
for (final dependencyId in packages) {
139143
final packageInternalAnalysis =

lib/src/solver/package_lister.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,5 +448,6 @@ class _RootSource extends BoundSource {
448448
@override
449449
Future<Pubspec> doDescribe(PackageId id) => throw _unsupported;
450450
@override
451-
String getDirectory(PackageId id) => throw _unsupported;
451+
String getDirectory(PackageId id, {String relativeFrom}) =>
452+
throw _unsupported;
452453
}

lib/src/source.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ abstract class BoundSource {
234234
/// Returns the directory where this package can (or could) be found locally.
235235
///
236236
/// If the source is cached, this will be a path in the system cache.
237-
String getDirectory(PackageId id);
237+
///
238+
/// If id is a relative path id, the directory will be relative from
239+
/// [relativeFrom]. Returns an absolute path if [relativeFrom] is not passed.
240+
String getDirectory(PackageId id, {String relativeFrom});
238241

239242
/// Returns metadata about a given package. Information about remotely hosted
240243
/// packages can be cached for up to [maxAge].

lib/src/source/cached.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ abstract class CachedSource extends BoundSource {
3131
/// Otherwise, defers to the subclass.
3232
@override
3333
Future<Pubspec> doDescribe(PackageId id) async {
34-
var packageDir = getDirectory(id);
34+
var packageDir = getDirectoryInCache(id);
3535
if (fileExists(path.join(packageDir, 'pubspec.yaml'))) {
3636
return Pubspec.load(packageDir, systemCache.sources,
3737
expectedName: id.name);
@@ -40,6 +40,12 @@ abstract class CachedSource extends BoundSource {
4040
return await describeUncached(id);
4141
}
4242

43+
@override
44+
String getDirectory(PackageId id, {String relativeFrom}) =>
45+
getDirectoryInCache(id);
46+
47+
String getDirectoryInCache(PackageId id);
48+
4349
/// Loads the (possibly remote) pubspec for the package version identified by
4450
/// [id].
4551
///
@@ -49,7 +55,7 @@ abstract class CachedSource extends BoundSource {
4955

5056
/// Determines if the package identified by [id] is already downloaded to the
5157
/// system cache.
52-
bool isInSystemCache(PackageId id) => dirExists(getDirectory(id));
58+
bool isInSystemCache(PackageId id) => dirExists(getDirectoryInCache(id));
5359

5460
/// Downloads the package identified by [id] to the system cache.
5561
Future<Package> downloadToSystemCache(PackageId id);

lib/src/source/git.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class BoundGitSource extends CachedSource {
323323

324324
/// Returns the path to the revision-specific cache of [id].
325325
@override
326-
String getDirectory(PackageId id) =>
326+
String getDirectoryInCache(PackageId id) =>
327327
p.join(_revisionCachePath(id), id.description['path']);
328328

329329
@override
@@ -396,7 +396,7 @@ class BoundGitSource extends CachedSource {
396396
result.add(RepairResult(id, success: false));
397397

398398
// Delete the revision cache path, not the subdirectory that contains the package.
399-
tryDeleteEntry(getDirectory(id));
399+
tryDeleteEntry(getDirectoryInCache(id));
400400
}
401401
}
402402

lib/src/source/hosted.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,12 @@ class BoundHostedSource extends CachedSource {
417417
@override
418418
Future<Package> downloadToSystemCache(PackageId id) async {
419419
if (!isInSystemCache(id)) {
420-
var packageDir = getDirectory(id);
420+
var packageDir = getDirectoryInCache(id);
421421
ensureDir(p.dirname(packageDir));
422422
await _download(id, packageDir);
423423
}
424424

425-
return Package.load(id.name, getDirectory(id), systemCache.sources);
425+
return Package.load(id.name, getDirectoryInCache(id), systemCache.sources);
426426
}
427427

428428
/// The system cache directory for the hosted source contains subdirectories
@@ -431,7 +431,7 @@ class BoundHostedSource extends CachedSource {
431431
/// Each of these subdirectories then contains a subdirectory for each
432432
/// package downloaded from that site.
433433
@override
434-
String getDirectory(PackageId id) {
434+
String getDirectoryInCache(PackageId id) {
435435
var parsed = source._parseDescription(id.description);
436436
var dir = _urlToDirectory(parsed.last);
437437
return p.join(systemCacheRoot, dir, '${parsed.first}-${id.version}');

lib/src/source/path.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ class BoundPathSource extends BoundSource {
181181
}
182182

183183
@override
184-
String getDirectory(PackageId id) => id.description['path'];
184+
String getDirectory(PackageId id, {String relativeFrom}) {
185+
return id.description['relative']
186+
? p.relative(id.description['path'], from: relativeFrom)
187+
: id.description['path'];
188+
}
185189

186190
/// Ensures that [description] is a valid path description and returns a
187191
/// normalized path to the package.

0 commit comments

Comments
 (0)