Skip to content

[2.x] feat: Add scriptedKeepTempDirectory setting#8621

Merged
eed3si9n merged 5 commits intosbt:developfrom
bitloi:feat/scripted-keep-temp-directory-3214
Jan 24, 2026
Merged

[2.x] feat: Add scriptedKeepTempDirectory setting#8621
eed3si9n merged 5 commits intosbt:developfrom
bitloi:feat/scripted-keep-temp-directory-3214

Conversation

@bitloi
Copy link
Copy Markdown
Contributor

@bitloi bitloi commented Jan 23, 2026

feat: Add scriptedKeepTempDirectory setting to preserve temp directories

Fixes #3214

Problem

When running scripted tests to debug sbt plugins, the temporary directories (/private/var/folder/...) are automatically deleted after tests complete. This makes it difficult to inspect the test state for debugging purposes, requiring workarounds like adding $ pause commands and manually copying directories.

Solution

Added a new scriptedKeepTempDirectory setting that allows users to preserve temporary directories after scripted tests complete. When enabled, the temporary directory path is logged so users can inspect it.

Usage:

scriptedKeepTempDirectory := true

Changes

  • ScriptedPlugin.scala: Added scriptedKeepTempDirectory setting key (defaults to false)
  • ScriptedRun.scala: Added keepTempDirectory parameter to run() method with backward-compatible versioning (V3 methods)
  • ScriptedTests.scala:
    • Added keepTempDirectory parameter to run(), runInParallel(), and batchScriptedRunner() methods
    • Modified createTestRunners() to conditionally use IO.createTemporaryDirectory instead of IO.withTemporaryDirectory when keepTempDirectory is true
    • Logs the temporary directory path when preserved

Testing

  • Compiles successfully
  • Binary compatible (new methods added, no existing signatures changed)
  • Backward compatible (defaults to false, existing behavior unchanged)

Checklist

  • Compiles successfully
  • scalafmtAll applied
  • Binary compatible (maintains backward compatibility)
  • Follows coding style guidelines

@bitloi
Copy link
Copy Markdown
Contributor Author

bitloi commented Jan 23, 2026

@eed3si9n Could you please review my PR?

Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! Overall the changes look good.
I probably need to grab this branch and test it manually.

@eed3si9n eed3si9n changed the title feat: Add scriptedKeepTempDirectory setting [2.x] feat: Add scriptedKeepTempDirectory setting Jan 23, 2026
@bitloi
Copy link
Copy Markdown
Contributor Author

bitloi commented Jan 23, 2026

@eed3si9n Can we merge this pr?

Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

I couldn't test this using sbt/sbt. For sbt/sbt's scripted test this funnels the call from sbt 1.x into sbt 2.x via reflection. Could you surface your new setting in project/Scripted.scala's ScriptedKeys, and fold that in to the scriptedTask. See also:

ThisBuild / scriptedBufferLog := true
ThisBuild / scriptedPrescripted := { _ => }

Thanks!

Added scriptedKeepTempDirectory to ScriptedKeys and updated scriptedTask
to pass the setting through the reflection bridge to sbt 2.x.

This allows the setting to be used from sbt 1.x when testing sbt 2.x.
@bitloi
Copy link
Copy Markdown
Contributor Author

bitloi commented Jan 23, 2026

I couldn't test this using sbt/sbt. For sbt/sbt's scripted test this funnels the call from sbt 1.x into sbt 2.x via reflection. Could you surface your new setting in project/Scripted.scala's ScriptedKeys, and fold that in to the scriptedTask. See also:

ThisBuild / scriptedBufferLog := true
ThisBuild / scriptedPrescripted := { _ => }

Thanks!

@eed3si9n Fixed. Could you please review again?

@bitloi bitloi requested a review from eed3si9n January 23, 2026 18:20
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

If I add ThisBuild / scriptedKeepTempDirectory := true to build.sbt, and run scripted actions/compile I see

[info] Running 1 / 1 (100.00%) scripted tests with LauncherBased(/Users/xxxx/sbt-launch.jar)
[info] Temporary directory for scripted tests: /var/folders/hg/2602nfrs2958vnshglyl3srw0000gn/T/sbt_6446e5ca
[info] Running actions/compile

but the directory only contains global cache. Does it work for you locally?

The temp directory was being created and logged, but test files were
being deleted after each test run. Now passes keepTempDirectory to
runBatchedTests and conditionally skips the cleanup logic.

This ensures test files remain in the temp directory for debugging.
@bitloi
Copy link
Copy Markdown
Contributor Author

bitloi commented Jan 23, 2026

I found the issue - the test files were being deleted after each test run regardless of the keepTempDirectory setting.

I've pushed a fix that:

  1. Passes keepTempDirectory through to runBatchedTests
  2. Conditionally skips the cleanup logic (lines 252-264) when keepTempDirectory is true

The cleanup code was deleting all files except global and global-logging directories after each test, which is why you only saw the global cache.

With this fix, when scriptedKeepTempDirectory := true, the test files should now remain in the temporary directory for debugging. Could you test it again?

@bitloi bitloi requested a review from eed3si9n January 23, 2026 21:24
Added check to ensure only one scripted test is requested when
scriptedKeepTempDirectory is true. This prevents batch test issues
where multiple tests would share the same temp directory and see
each other's files.
@bitloi
Copy link
Copy Markdown
Contributor Author

bitloi commented Jan 23, 2026

Fixed, please review it again.

Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

Thanks!

@eed3si9n eed3si9n merged commit f870475 into sbt:develop Jan 24, 2026
14 checks passed
eed3si9n pushed a commit that referenced this pull request Jan 29, 2026
In PR #8621, I added a new `keepTempDirectory` parameter to `ScriptedRun.run()` and `invoke()` methods. To suppress MiMa warnings, I added `mimaBinaryIssueFilters` for:
- `DirectMissingMethodProblem("sbt.ScriptedRun.run")`
- `DirectMissingMethodProblem("sbt.ScriptedRun.invoke")`
- `DirectMissingMethodProblem` for various internal `RunV1`, `RunV2`, `RunInParallelV1`, `RunInParallelV2` classes

However, this broke binary compatibility, which prevents sbt 1.x from calling sbt 2.x for cross-building (building sbt 2.x plugins using sbt 1.x).
@xuwei-k
Copy link
Copy Markdown
Member

xuwei-k commented Feb 16, 2026

@mkurz
Copy link
Copy Markdown
Member

mkurz commented Feb 22, 2026

Very useful feature, thanks!

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.

Scripted option to not remove temporary directory

4 participants