diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java | 50 |
1 files changed, 28 insertions, 22 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java index 0bd16ae90a..b3eb957019 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java @@ -27,7 +27,7 @@ import com.google.devtools.build.lib.skyframe.ASTFileLookupValue.ASTLookupInputE import com.google.devtools.build.lib.syntax.BuildFileAST; import com.google.devtools.build.lib.syntax.Label; import com.google.devtools.build.lib.syntax.Label.SyntaxException; -import com.google.devtools.build.lib.syntax.SkylarkEnvironment; +import com.google.devtools.build.lib.syntax.Mutability; import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.skyframe.SkyFunction; import com.google.devtools.build.skyframe.SkyFunctionException; @@ -76,7 +76,7 @@ public class SkylarkImportLookupFunction implements SkyFunction { throw new SkylarkImportLookupFunctionException(SkylarkImportFailedException.noFile(file)); } - Map<PathFragment, SkylarkEnvironment> importMap = new HashMap<>(); + Map<PathFragment, com.google.devtools.build.lib.syntax.Environment> importMap = new HashMap<>(); ImmutableList.Builder<SkylarkFileDependency> fileDependencies = ImmutableList.builder(); BuildFileAST ast = astLookupValue.getAST(); // TODO(bazel-team): Refactor this code and PackageFunction to reduce code duplications. @@ -114,10 +114,11 @@ public class SkylarkImportLookupFunction implements SkyFunction { file)); } - SkylarkEnvironment extensionEnv = createEnv(ast, file, importMap, env); - // Skylark UserDefinedFunctions are sharing function definition Environments, so it's extremely - // important not to modify them from this point. Ideally they should be only used to import - // symbols and serve as global Environments of UserDefinedFunctions. + // Skylark UserDefinedFunction-s in that file will share this function definition Environment, + // which will be frozen by the time it is returned by createEnv. + com.google.devtools.build.lib.syntax.Environment extensionEnv = + createEnv(ast, file, importMap, env); + return new SkylarkImportLookupValue( extensionEnv, new SkylarkFileDependency(label, fileDependencies.build())); } @@ -164,29 +165,34 @@ public class SkylarkImportLookupFunction implements SkyFunction { } /** - * Creates the SkylarkEnvironment to be imported. After it's returned, the Environment - * must not be modified. + * Creates the Environment to be imported. + * After it's returned, the Environment must not be modified. */ - private SkylarkEnvironment createEnv(BuildFileAST ast, PathFragment file, - Map<PathFragment, SkylarkEnvironment> importMap, Environment env) + private com.google.devtools.build.lib.syntax.Environment createEnv( + BuildFileAST ast, + PathFragment file, + Map<PathFragment, com.google.devtools.build.lib.syntax.Environment> importMap, + Environment env) throws InterruptedException, SkylarkImportLookupFunctionException { StoredEventHandler eventHandler = new StoredEventHandler(); // TODO(bazel-team): this method overestimates the changes which can affect the // Skylark RuleClass. For example changes to comments or unused functions can modify the hash. // A more accurate - however much more complicated - way would be to calculate a hash based on // the transitive closure of the accessible AST nodes. - SkylarkEnvironment extensionEnv = ruleClassProvider - .createSkylarkRuleClassEnvironment(eventHandler, ast.getContentHashCode()); - extensionEnv.update("native", packageFactory.getNativeModule()); - extensionEnv.setImportedExtensions(importMap); - ast.exec(extensionEnv, eventHandler); - SkylarkRuleClassFunctions.exportRuleFunctions(extensionEnv, file); - - Event.replayEventsOn(env.getListener(), eventHandler.getEvents()); - if (eventHandler.hasErrors()) { - throw new SkylarkImportLookupFunctionException(SkylarkImportFailedException.errors(file)); - } - return extensionEnv; + try (Mutability mutability = Mutability.create("importing %s", file)) { + com.google.devtools.build.lib.syntax.Environment extensionEnv = + ruleClassProvider.createSkylarkRuleClassEnvironment( + mutability, eventHandler, ast.getContentHashCode(), importMap) + .setupOverride("native", packageFactory.getNativeModule()); + ast.exec(extensionEnv, eventHandler); + SkylarkRuleClassFunctions.exportRuleFunctions(extensionEnv, file); + + Event.replayEventsOn(env.getListener(), eventHandler.getEvents()); + if (eventHandler.hasErrors()) { + throw new SkylarkImportLookupFunctionException(SkylarkImportFailedException.errors(file)); + } + return extensionEnv; + } } @Override |