Skip to content

fix: make build cache relocatable when using Baseline by storing relative paths#1074

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-build-cache-relocatability
Open

fix: make build cache relocatable when using Baseline by storing relative paths#1074
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-build-cache-relocatability

Conversation

Copilot AI commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

When using a shared build cache across different workspace directories, LintErrorResult serialized lintedFile as an absolute path. On cache restoration in a relocated project, baseline filtering broke because toRelativeString(newProjectDir) on a stale absolute path produced a path like ../../original/src/... instead of src/..., causing all baseline-suppressed errors to surface as real failures.

Changes

  • LintErrorResult — bumped serialVersionUID to invalidate stale cached binaries containing absolute paths
  • KtLintWorkAction — added projectDirectory parameter; converts each linted file to a project-relative path before writing to the .bin output:
    LintErrorResult(File(it.toRelativeString(projectDir)), result.lintErrors)
  • BaseKtLintCheckTask — stores projectDir eagerly; passes it to the work action; resolves stored relative paths back to absolute when loading prior errors for incremental runs
  • ConsoleReportWorkAction / GenerateReportsWorkAction — resolve lintedFile against projectDir for absolute-path operations (logging, reporter output); use lintedFile.path directly for baseline map lookup (already relative)
  • GenerateBaselineWorkAction — uses lintedFile.path directly instead of toRelativeString(projectDir) since the path is now already relative
  • BuildCacheTest — adds checkWithBaselineIsRelocatable: generates a baseline, primes the cache in the original directory, then verifies both runKtlintCheckOverMainSourceSet and ktlintMainSourceSetCheck are FROM_CACHE in the relocated project

…catable with baseline

When using a shared build cache across different workspace directories with
a baseline.xml, the cached error binary reports (.bin) contained absolute
file paths. This caused the baseline filtering logic to fail matching cached
errors against baseline entries, resulting in unexpected build failures.

The fix stores file paths relative to the project directory in LintErrorResult.
- KtLintWorkAction now converts absolute paths to relative before serializing
- BaseKtLintCheckTask passes project directory to the work action and resolves
  relative paths back to absolute when loading previous errors
- ConsoleReportWorkAction and GenerateReportsWorkAction resolve relative paths
  for absolute path operations (logging/reporting) while using them directly
  for baseline lookups
- GenerateBaselineWorkAction uses the already-relative lintedFile.path directly
- serialVersionUID bumped to invalidate stale cached data

Fixes #1041
Copilot AI changed the title [WIP] Fix build cache relocatability in ktlint-gradle fix: make build cache relocatable when using Baseline by storing relative paths Jun 15, 2026
Copilot AI requested a review from JLLeitschuh June 15, 2026 16:36
@JLLeitschuh

Copy link
Copy Markdown
Owner

@copilot please fix the requirement for a change log file entry.

Please remember that this needs to be added every time copilot creates a pull request

Copilot AI commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

@copilot please fix the requirement for a change log file entry.

Please remember that this needs to be added every time copilot creates a pull request

Added a CHANGELOG.md entry under [Unreleased] in commit chore: add CHANGELOG.md entry for PR #1074.

@JLLeitschuh JLLeitschuh left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable to me. @wakingrufus what do you think?

@JLLeitschuh JLLeitschuh marked this pull request as ready for review June 18, 2026 17:56
) : DefaultTask(),
PatternFilterable {

private val projectDir: File = projectLayout.projectDirectory.asFile

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should be a proper @InputDirectory property, and we should probably remove projectLayout from the constructor parameters and move the logic for setting properties derived from the layout to the plugin code which creates these tasks. this will make it work better with configuration cache

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make the entire projectDir an @InputDirectory won't that mean that the whole directory is tracked as an input? Thus any changes to any subfile, not just kotlin files would invalidate the task?

@copilot thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that concern is valid. Annotating projectDirectory with @InputDirectory would cause Gradle to fingerprint every file under the project root, so any change to any file — including non-Kotlin files — would invalidate the task. That's exactly the kind of over-tracking we want to avoid.

The projectDirectory is only used as an anchor for computing relative paths (not as a content input), so the right annotation is @Internal. The actual content-based inputs are already tracked precisely via @get:InputFiles source with PathSensitivity.RELATIVE.

@wakingrufus's broader point about moving layout-derived property initialization out of the constructor and into the plugin's task-registration code is still a good one for configuration-cache hygiene, but that's orthogonal to the annotation question — and for the path-anchor use-case, @Internal is the correct choice regardless of where the value is set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Build cache is not relocatable when using Baseline due to absolute paths in serialized reports

3 participants