Skip to content

Commit

Permalink
Use relative path in package_config.json when PUB_CACHE is relative (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm authored Sep 5, 2024
1 parent 9f93040 commit 6b48f43
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib/src/source/cached.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ abstract class CachedSource extends Source {
PackageId id,
SystemCache cache, {
String? relativeFrom,
}) =>
getDirectoryInCache(id, cache);
}) {
final dir = getDirectoryInCache(id, cache);
if (p.isRelative(dir)) {
return p.relative(dir, from: relativeFrom);
}
return dir;
}

String getDirectoryInCache(PackageId id, SystemCache cache);

Expand Down
5 changes: 3 additions & 2 deletions lib/src/system_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ class SystemCache {
String get tempDir => p.join(rootDir, '_temp');

static String defaultDir = (() {
if (Platform.environment.containsKey('PUB_CACHE')) {
return p.absolute(Platform.environment['PUB_CACHE']!);
final envCache = Platform.environment['PUB_CACHE'];
if (envCache != null) {
return envCache;
} else if (Platform.isWindows) {
// %LOCALAPPDATA% is used as the cache location over %APPDATA%, because
// the latter is synchronised between devices when the user roams between
Expand Down
35 changes: 34 additions & 1 deletion test/package_config_file_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 'package:path/path.dart' as p;
import 'package:pub/src/exit_codes.dart' as exit_codes;

import 'package:pub/src/package_config.dart';
import 'package:test/test.dart';

import 'descriptor.dart' as d;
Expand Down Expand Up @@ -60,6 +61,38 @@ void main() {
]).validate();
});

test('package_config.json uses relative paths if PUB_CACHE is relative',
() async {
final server = await servePackages();
server.serve('foo', '1.2.3');

await d.dir(appPath, [
d.appPubspec(dependencies: {'foo': '1.2.3'}),
]).create();

await pubCommand(command, environment: {'PUB_CACHE': './pub_cache'});

await d.dir(appPath, [
d.packageConfigFile(
[
PackageConfigEntry(
name: 'foo',
rootUri: p.toUri(
'../pub_cache/hosted/localhost%58${globalServer.port}/foo-1.2.3',
),
packageUri: Uri.parse('lib/'),
),
d.packageConfigEntry(
name: 'myapp',
path: '.',
languageVersion: '3.0',
),
],
pubCache: p.join(d.sandbox, appPath, 'pub_cache'),
),
]).validate();
});

test('package_config.json file is overwritten', () async {
await servePackages()
..serve(
Expand Down

0 comments on commit 6b48f43

Please sign in to comment.