Skip to content

Commit

Permalink
Use an internal option to check for disabled caching for artifact tra…
Browse files Browse the repository at this point in the history
…nsforms
  • Loading branch information
lptr committed Apr 19, 2024
1 parent f3e68a7 commit 8bf667c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import org.gradle.internal.authentication.AuthenticationSchemeRegistry;
import org.gradle.internal.build.BuildModelLifecycleListener;
import org.gradle.internal.build.BuildState;
import org.gradle.internal.buildoption.InternalOptions;
import org.gradle.internal.component.ResolutionFailureHandler;
import org.gradle.internal.component.external.model.JavaEcosystemVariantDerivationStrategy;
import org.gradle.internal.component.external.model.ModuleComponentArtifactMetadata;
Expand Down Expand Up @@ -254,6 +255,7 @@ public Supplier<File> getReservedFileSystemLocation() {
TransformInvocationFactory createTransformInvocationFactory(
ExecutionEngine executionEngine,
FileSystemAccess fileSystemAccess,
InternalOptions internalOptions,
ImmutableTransformWorkspaceServices transformWorkspaceServices,
TransformExecutionListener transformExecutionListener,
FileCollectionFactory fileCollectionFactory,
Expand All @@ -264,6 +266,7 @@ TransformInvocationFactory createTransformInvocationFactory(
return new DefaultTransformInvocationFactory(
executionEngine,
fileSystemAccess,
internalOptions,
transformExecutionListener,
transformWorkspaceServices,
fileCollectionFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import javax.annotation.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -62,7 +60,6 @@

abstract class AbstractTransformExecution implements UnitOfWork {
private static final CachingDisabledReason NOT_CACHEABLE = new CachingDisabledReason(CachingDisabledReasonCategory.NOT_CACHEABLE, "Caching not enabled.");
private static final String CACHING_DISABLED_PROPERTY = "org.gradle.internal.transform-caching-disabled";
private static final CachingDisabledReason CACHING_DISABLED_REASON = new CachingDisabledReason(CachingDisabledReasonCategory.NOT_CACHEABLE, "Caching disabled by property (experimental)");

protected static final String INPUT_ARTIFACT_PROPERTY_NAME = "inputArtifact";
Expand All @@ -86,6 +83,7 @@ abstract class AbstractTransformExecution implements UnitOfWork {

private final Provider<FileSystemLocation> inputArtifactProvider;
protected final InputFingerprinter inputFingerprinter;
private final boolean disableCachingByProeprty;

private BuildOperationContext operationContext;

Expand All @@ -99,7 +97,9 @@ protected AbstractTransformExecution(
BuildOperationRunner buildOperationRunner,
BuildOperationProgressEventEmitter progressEventEmitter,
FileCollectionFactory fileCollectionFactory,
InputFingerprinter inputFingerprinter
InputFingerprinter inputFingerprinter,

boolean disableCachingByProeprty
) {
this.transform = transform;
this.inputArtifact = inputArtifact;
Expand All @@ -112,6 +112,7 @@ protected AbstractTransformExecution(
this.progressEventEmitter = progressEventEmitter;
this.fileCollectionFactory = fileCollectionFactory;
this.inputFingerprinter = inputFingerprinter;
this.disableCachingByProeprty = disableCachingByProeprty;
}

@Override
Expand Down Expand Up @@ -302,26 +303,13 @@ public Optional<CachingDisabledReason> shouldDisableCaching(@Nullable Overlappin
}

private Optional<CachingDisabledReason> maybeDisableCachingByProperty() {
if (isCachingDisabledByProperty()) {
if (disableCachingByProeprty) {
return Optional.of(CACHING_DISABLED_REASON);
}

return Optional.empty();
}

private boolean isCachingDisabledByProperty() {
String experimentalProperty = System.getProperty(CACHING_DISABLED_PROPERTY);
if (experimentalProperty != null) {
if (experimentalProperty.isEmpty()) {
return true;
}
List<String> disabledTransformClasses = Arrays.asList(experimentalProperty.split(","));
return disabledTransformClasses.contains(transform.getImplementationClass().getName());
}

return false;
}

@Override
public String getDisplayName() {
return transform.getDisplayName() + ": " + inputArtifact;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.gradle.cache.Cache;
import org.gradle.internal.Deferrable;
import org.gradle.internal.Try;
import org.gradle.internal.buildoption.InternalOptions;
import org.gradle.internal.buildoption.StringInternalOption;
import org.gradle.internal.execution.ExecutionEngine;
import org.gradle.internal.execution.ExecutionEngine.IdentityCacheResult;
import org.gradle.internal.execution.InputFingerprinter;
Expand All @@ -37,11 +39,15 @@

import javax.annotation.Nullable;
import java.io.File;
import java.util.Arrays;
import java.util.List;

public class DefaultTransformInvocationFactory implements TransformInvocationFactory {
private static final StringInternalOption CACHING_DISABLED_PROPERTY = new StringInternalOption("org.gradle.internal.transform-caching-disabled", "");

private final ExecutionEngine executionEngine;
private final FileSystemAccess fileSystemAccess;
private final InternalOptions internalOptions;
private final TransformExecutionListener transformExecutionListener;
private final ImmutableTransformWorkspaceServices immutableWorkspaceServices;
private final FileCollectionFactory fileCollectionFactory;
Expand All @@ -52,6 +58,7 @@ public class DefaultTransformInvocationFactory implements TransformInvocationFac
public DefaultTransformInvocationFactory(
ExecutionEngine executionEngine,
FileSystemAccess fileSystemAccess,
InternalOptions internalOptions,
TransformExecutionListener transformExecutionListener,
ImmutableTransformWorkspaceServices immutableWorkspaceServices,
FileCollectionFactory fileCollectionFactory,
Expand All @@ -61,6 +68,7 @@ public DefaultTransformInvocationFactory(
) {
this.executionEngine = executionEngine;
this.fileSystemAccess = fileSystemAccess;
this.internalOptions = internalOptions;
this.transformExecutionListener = transformExecutionListener;
this.immutableWorkspaceServices = immutableWorkspaceServices;
this.fileCollectionFactory = fileCollectionFactory;
Expand All @@ -82,6 +90,8 @@ public Deferrable<Try<ImmutableList<File>>> createInvocation(
Cache<Identity, IdentityCacheResult<TransformWorkspaceResult>> identityCache;
UnitOfWork execution;

boolean cachingDisabledByProperty = isCachingDisabledByProperty(transform);

// TODO This is a workaround for script compilation that is triggered via the "early" execution
// engine created in DependencyManagementBuildScopeServices. We should unify the execution
// engines instead.
Expand All @@ -103,7 +113,9 @@ public Deferrable<Try<ImmutableList<File>>> createInvocation(
fileCollectionFactory,
inputFingerprinter,
fileSystemAccess,
immutableWorkspaceServices.getWorkspaceProvider()
immutableWorkspaceServices.getWorkspaceProvider(),

cachingDisabledByProperty
);
effectiveEngine = executionEngine;
} else {
Expand All @@ -122,7 +134,9 @@ public Deferrable<Try<ImmutableList<File>>> createInvocation(
progressEventEmitter,
fileCollectionFactory,
inputFingerprinter,
immutableWorkspaceServices.getWorkspaceProvider()
immutableWorkspaceServices.getWorkspaceProvider(),

cachingDisabledByProperty
);
} else {
// Incremental project artifact transforms run in project-bound mutable workspace
Expand All @@ -140,7 +154,9 @@ public Deferrable<Try<ImmutableList<File>>> createInvocation(
progressEventEmitter,
fileCollectionFactory,
inputFingerprinter,
workspaceServices.getWorkspaceProvider()
workspaceServices.getWorkspaceProvider(),

cachingDisabledByProperty
);
}
}
Expand All @@ -160,4 +176,17 @@ private ProjectInternal determineProducerProject(TransformStepSubject subject) {
return null;
}
}

private boolean isCachingDisabledByProperty(Transform transform) {
String experimentalProperty = internalOptions.getOption(CACHING_DISABLED_PROPERTY).get();
if (experimentalProperty != null) {
if (experimentalProperty.isEmpty()) {
return true;
}
List<String> disabledTransformClasses = Arrays.asList(experimentalProperty.split(","));
return disabledTransformClasses.contains(transform.getImplementationClass().getName());
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ public MutableTransformExecution(
BuildOperationProgressEventEmitter progressEventEmitter,
FileCollectionFactory fileCollectionFactory,
InputFingerprinter inputFingerprinter,
MutableWorkspaceProvider workspaceProvider
MutableWorkspaceProvider workspaceProvider,

boolean disableCachingByProperty
) {
super(
transform, inputArtifact, dependencies, subject,
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter,
disableCachingByProperty
);
this.rootProjectLocation = producerProject.getRootDir().getAbsolutePath() + File.separator;
this.producerBuildTreePath = producerProject.getBuildTreePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ public NonNormalizedIdentityImmutableTransformExecution(
FileCollectionFactory fileCollectionFactory,
InputFingerprinter inputFingerprinter,
FileSystemAccess fileSystemAccess,
ImmutableWorkspaceProvider workspaceProvider
ImmutableWorkspaceProvider workspaceProvider,

boolean disableCachingByProperty
) {
super(
transform, inputArtifact, dependencies, subject,
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter,
disableCachingByProperty
);
this.fileSystemAccess = fileSystemAccess;
this.workspaceProvider = workspaceProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ public NormalizedIdentityImmutableTransformExecution(
BuildOperationProgressEventEmitter progressEventEmitter,
FileCollectionFactory fileCollectionFactory,
InputFingerprinter inputFingerprinter,
ImmutableWorkspaceProvider workspaceProvider
ImmutableWorkspaceProvider workspaceProvider,

boolean disableCachingByProperty
) {
super(
transform, inputArtifact, dependencies, subject,
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter
transformExecutionListener, buildOperationRunner, progressEventEmitter, fileCollectionFactory, inputFingerprinter,
disableCachingByProperty
);
this.workspaceProvider = workspaceProvider;
}
Expand Down

0 comments on commit 8bf667c

Please sign in to comment.