Skip to content

Commit

Permalink
Use .dart_tool/pub as the cache directory
Browse files Browse the repository at this point in the history
Pub will continue to read cached files from .pub if it exists. Any
time it modifies the cache, it will migrate .pub to .dart_tool/pub.

Closes #1795
  • Loading branch information
nex3 committed Feb 2, 2018
1 parent 0d9a965 commit 68a6c12
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.buildlog
.DS_Store
.idea
.pub/
.dart_tool/
.settings/
/build/
packages
Expand Down
3 changes: 2 additions & 1 deletion lib/src/barback/asset_environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class AssetEnvironment {
new Map.fromIterable(packages, value: (packageName) {
var package = graph.packages[packageName];
if (mode != BarbackMode.DEBUG) return package;
var cache = path.join('.pub/deps/debug', packageName);
var cache =
path.join(graph.entrypoint.cachePath, 'deps/debug', packageName);
if (!dirExists(cache)) return package;
return new CachedPackage(package, cache);
}));
Expand Down
6 changes: 3 additions & 3 deletions lib/src/barback/transformer_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class TransformerCache {
/// Loads the transformer cache for [environment].
///
/// This may modify the cache.
TransformerCache.load(PackageGraph graph)
: _graph = graph,
_dir = graph.entrypoint.root.path(".pub/transformers") {
TransformerCache.load(PackageGraph graph) : _graph = graph {
graph.entrypoint.migrateCache();
_dir = p.join(graph.entrypoint.cachePath, "transformers");
_oldTransformers = _parseManifest();
}

Expand Down
33 changes: 31 additions & 2 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,26 @@ class Entrypoint {
/// The path to the entrypoint package's lockfile.
String get lockFilePath => root.path('pubspec.lock');

/// The path to the entrypoint package's `.dart_tool/pub` cache directory.
///
/// If the old-style `.pub` directory is being used, this returns that
/// instead.
String get cachePath {
var newPath = root.path('.dart_tool/pub');
var oldPath = root.path('.pub');
if (!dirExists(newPath) && dirExists(oldPath)) return oldPath;
return newPath;
}

/// The path to the directory containing precompiled dependencies.
///
/// We just precompile the debug version of a package. We're mostly interested
/// in improving speed for development iteration loops, which usually use
/// debug mode.
String get _precompiledDepsPath => root.path('.pub', 'deps', 'debug');
String get _precompiledDepsPath => p.join(cachePath, 'deps', 'debug');

/// The path to the directory containing dependency executable snapshots.
String get _snapshotPath => root.path('.pub', 'bin');
String get _snapshotPath => p.join(cachePath, 'bin');

/// Loads the entrypoint from a package at [rootDir].
Entrypoint(String rootDir, SystemCache cache, {this.isGlobal: false})
Expand Down Expand Up @@ -370,6 +381,7 @@ class Entrypoint {
/// Precompiles all executables from dependencies that don't transitively
/// depend on [this] or on a path dependency.
Future precompileExecutables({Iterable<String> changed}) async {
migrateCache();
_deleteExecutableSnapshots(changed: changed);

var executables = new Map<String, List<AssetId>>.fromIterable(
Expand Down Expand Up @@ -785,4 +797,21 @@ class Entrypoint {
if (entryExists(symlink)) deleteEntry(symlink);
if (packagesDir) createSymlink(packagesPath, symlink, relative: true);
}

/// If the entrypoint uses the old-style `.pub` cache directory, migrates it
/// to the new-style `.dart_tool/pub` directory.
void migrateCache() {
var oldPath = root.path('.pub');
if (!dirExists(oldPath)) return;

var newPath = root.path('.dart_tool/pub');

// If both the old and new directories exist, something weird is going on.
// Do nothing to avoid making things worse. Pub will prefer the new
// directory anyway.
if (dirExists(newPath)) return;

ensureDir(p.dirname(newPath));
renameDir(oldPath, newPath);
}
}
4 changes: 3 additions & 1 deletion lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Future<int> runExecutable(Entrypoint entrypoint, String package,
}
}

entrypoint.migrateCache();

// Unless the user overrides the verbosity, we want to filter out the
// normal pub output shown while loading the environment.
if (log.verbosity == log.Verbosity.NORMAL) {
Expand All @@ -60,7 +62,7 @@ Future<int> runExecutable(Entrypoint entrypoint, String package,
if (p.extension(executable) != ".dart") executable += ".dart";

var localSnapshotPath =
p.join(".pub", "bin", package, "$executable.snapshot");
p.join(entrypoint.cachePath, "bin", package, "$executable.snapshot");
if (!isGlobal &&
fileExists(localSnapshotPath) &&
// Dependencies are only snapshotted in release mode, since that's the
Expand Down
4 changes: 1 addition & 3 deletions lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,6 @@ final bool runningFromSdk =
final _dartRepoRegExp = new RegExp(r"/third_party/pkg/pub/("
r"bin/pub\.dart"
r"|"
r"\.pub/pub\.test\.snapshot"
r"|"
r"test/.*_test\.dart"
r")$");

Expand Down Expand Up @@ -584,7 +582,7 @@ final String pubRoot = (() {
return path.joinAll(components.take(testIndex));
}

// Pub is either run from ".pub/pub.test.snapshot" or "bin/pub.dart".
// Pub is run from "bin/pub.dart".
return path.dirname(path.dirname(script));
})();

Expand Down
2 changes: 1 addition & 1 deletion lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Package {
/// This is similar to `p.join(dir, part1, ...)`, except that subclasses may
/// override it to report that certain paths exist elsewhere than within
/// [dir]. For example, a [CachedPackage]'s `lib` directory is in the
/// `.pub/deps` directory.
/// `.dart_tool/pub/deps` directory.
String path(String part1,
[String part2,
String part3,
Expand Down
117 changes: 102 additions & 15 deletions test/get/cache_transformed_dependency_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'package:pub/src/io.dart';

import '../descriptor.dart' as d;
import '../serve/utils.dart';
import '../test_pub.dart';
Expand Down Expand Up @@ -115,7 +117,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();
});
Expand Down Expand Up @@ -146,7 +148,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();
});
Expand All @@ -164,7 +166,7 @@ main() {

await pubGet(output: isNot(contains("Precompiled foo.")));

await d.dir(appPath, [d.nothing(".pub/deps")]).validate();
await d.dir(appPath, [d.nothing(".dart_tool/pub/deps")]).validate();
});

test("recaches when the dependency is updated", () async {
Expand Down Expand Up @@ -199,7 +201,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();

Expand All @@ -209,7 +211,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'See ya!';")])
]).validate();
});
Expand Down Expand Up @@ -286,7 +288,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final mode = 'debug';")])
]).validate();
});
Expand Down Expand Up @@ -320,7 +322,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Modified!';")])
]).create();

Expand Down Expand Up @@ -357,6 +359,51 @@ main() {

await pubGet(output: contains("Precompiled foo."));

// Manually reset the cache to its original state to prove that the
// transformer won't be run again on it.
await d.dir(appPath, [
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Hello!';")])
]).create();

var pub = await pubRun(args: ["bin/script"]);
expect(pub.stdout, emits("Hello!"));
await pub.shouldExit();
});

test("reads cached packages from the old-style cache", () async {
await servePackages((builder) {
builder.serveRealPackage('barback');

builder.serve("foo", "1.2.3", deps: {
'barback': 'any'
}, pubspec: {
'transformers': ['foo']
}, contents: [
d.dir("lib", [
d.file("transformer.dart", replaceTransformer("Hello", "Goodbye")),
d.file("foo.dart", "final message = 'Hello!';")
])
]);
});

await d.dir(appPath, [
d.appPubspec({"foo": "1.2.3"}),
d.dir('bin', [
d.file('script.dart', """
import 'package:foo/foo.dart';
void main() => print(message);""")
])
]).create();

await pubGet(output: contains("Precompiled foo."));

// Move the directory to the old location to simulate it being created by an
// older version of pub.
renameDir(p.join(d.sandbox, appPath, '.dart_tool', 'pub'),
p.join(d.sandbox, appPath, '.pub'));

// Manually reset the cache to its original state to prove that the
// transformer won't be run again on it.
await d.dir(appPath, [
Expand All @@ -369,6 +416,42 @@ main() {
await pub.shouldExit();
});

test("migrates the old-style cache", () async {
await servePackages((builder) {
builder.serveRealPackage('barback');

builder.serve("foo", "1.2.3", deps: {
'barback': 'any'
}, pubspec: {
'transformers': ['foo']
}, contents: [
d.dir("lib", [
d.file("transformer.dart", replaceTransformer("Hello", "Goodbye")),
d.file("foo.dart", "final message = 'Hello!';")
])
]);
});

await d.dir(appPath, [
d.appPubspec({"foo": "1.2.3"}),

// Simulate an old-style cache directory.
d.dir(".pub", [d.file("junk", "junk")])
]).create();

await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [d.nothing(".pub")]).validate();

await d.dir(appPath, [
d.dir(".dart_tool/pub", [
d.file("junk", "junk"),
d.dir("deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
])
]).validate();
});

// Regression test for issue 21087.
test("hasInput works for static packages", () async {
await servePackages((builder) {
Expand Down Expand Up @@ -436,7 +519,7 @@ main() {
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();

Expand All @@ -445,7 +528,8 @@ main() {

await pubGet(output: isNot(contains("Precompiled foo.")));

await d.dir(appPath, [d.nothing(".pub/deps/debug/foo")]).validate();
await d
.dir(appPath, [d.nothing(".dart_tool/pub/deps/debug/foo")]).validate();
});

// Regression test for https://github.com/dart-lang/pub/issues/1586.
Expand All @@ -472,7 +556,8 @@ main() {
await pubGet(output: contains("Precompiled foo"));

await d.dir(appPath, [
d.file(".pub/deps/debug/foo/lib/inputs.txt", contains('hello.dart.copy'))
d.file(".dart_tool/pub/deps/debug/foo/lib/inputs.txt",
contains('hello.dart.copy'))
]).validate();

await pubServe();
Expand Down Expand Up @@ -508,7 +593,7 @@ foo|lib/list_transformer.dart.copy""");
await pubGet(
args: ["--no-precompile"], output: isNot(contains("Precompiled")));

await d.nothing(p.join(appPath, ".pub")).validate();
await d.nothing(p.join(appPath, ".dart_tool/pub")).validate();
});

test("deletes the cache when the dependency is updated", () async {
Expand Down Expand Up @@ -543,7 +628,7 @@ foo|lib/list_transformer.dart.copy""");
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();

Expand All @@ -553,7 +638,9 @@ foo|lib/list_transformer.dart.copy""");
await pubGet(
args: ["--no-precompile"], output: isNot(contains("Precompiled")));

await d.nothing(p.join(appPath, ".pub/deps/debug/foo")).validate();
await d
.nothing(p.join(appPath, ".dart_tool/pub/deps/debug/foo"))
.validate();
});

test(
Expand All @@ -580,7 +667,7 @@ foo|lib/list_transformer.dart.copy""");
await pubGet(output: contains("Precompiled foo."));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();

Expand All @@ -589,7 +676,7 @@ foo|lib/list_transformer.dart.copy""");
args: ["--no-precompile"], output: isNot(contains("Precompiled")));

await d.dir(appPath, [
d.dir(".pub/deps/debug/foo/lib",
d.dir(".dart_tool/pub/deps/debug/foo/lib",
[d.file("foo.dart", "final message = 'Goodbye!';")])
]).validate();
});
Expand Down
Loading

0 comments on commit 68a6c12

Please sign in to comment.