Skip to content

fix(runtest): don't run in all contexts#13930

Merged
Alizter merged 1 commit intoocaml:mainfrom
Alizter:push-ououwvzupysq
Mar 30, 2026
Merged

fix(runtest): don't run in all contexts#13930
Alizter merged 1 commit intoocaml:mainfrom
Alizter:push-ououwvzupysq

Conversation

@Alizter
Copy link
Copy Markdown
Collaborator

@Alizter Alizter commented Mar 26, 2026

When choosing which context to find and run our tests, we always choose the default context and if that is not available a unique context. When there is no good choice we error.

Copy link
Copy Markdown
Member

@rgrinberg rgrinberg left a comment

Choose a reason for hiding this comment

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

A couple of things:

  1. If we're going to pick a context in a semi random way, we should document the rules for doing so. I.e. something like the "first context that occurs" in the workspace file.

  2. Did we consider just improving the error message? I don't understand why we should making a decision for the user at all here and tests can certainly be defined in a context dependant way.

@Alizter Alizter marked this pull request as draft March 26, 2026 19:14
@Alizter Alizter force-pushed the push-ououwvzupysq branch from a49b4c3 to 7f7743f Compare March 27, 2026 09:01
@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 27, 2026

  1. If we're going to pick a context in a semi random way, we should document the rules for doing so. I.e. something like the "first context that occurs" in the workspace file.

I've reverted my changes picking a context since I was also not happy with it. I've now gone with finding tests per-(super)context.

  1. Did we consider just improving the error message? I don't understand why we should making a decision for the user at all here and tests can certainly be defined in a context dependant way.

This is technically a regression, since something that worked before is no longer working. See #13904. Therefore introducing an error message, especially since in the oxcaml case there is an obvious context to pick, seems like a bad user experience.

For context, oxcaml have been running dune runtest on its own and they have a main context and no default context. There are a few other places in dune that would also break due to making similar assumptions.

At the time of implementing dune runtest for ml tests, it seemed like a sensible choice to pick default, I can see now that is not a good idea.

@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 27, 2026

and tests can certainly be defined in a context dependant way.

Just so we are on the same page, could you elaborate on what you have in mind here?

@Alizter Alizter requested a review from rgrinberg March 27, 2026 09:08
@Alizter Alizter changed the title fix(runtest): choose a sensible context for test classification fix(runtest): classify tests per-context Mar 27, 2026
@Alizter Alizter force-pushed the push-ououwvzupysq branch from 7f7743f to 8df5696 Compare March 27, 2026 09:11
@Leonidas-from-XIV
Copy link
Copy Markdown
Collaborator

This is technically a regression, since something that worked before is no longer working.

What was it doing before? I am worried that the behavior before was not reasonable, thus it could make more sense to error out now with a sensible error message instead of silently doing the wrong thing.

@Alizter Alizter requested a review from ElectreAAS March 27, 2026 09:12
@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 27, 2026

@Leonidas-from-XIV Before we started running ml tests, we did not need to know anything about contexts. They were threaded throughout and used by the existing context handling mechanisms.

Since #13064 we now have to classify ml tests, this requires a super context to do so since we use dir_contents to load ml sources.

In 3.22.0 we are picking an arbitrary context in order to carry this out, this fix we will do the classification for each separate context, avoiding the need for a choice.

@Alizter Alizter marked this pull request as ready for review March 27, 2026 09:46
@Alizter Alizter force-pushed the push-ououwvzupysq branch from 8df5696 to 4d80b08 Compare March 27, 2026 09:51
@rgrinberg
Copy link
Copy Markdown
Member

I don't think we should fixate on this being a regression. If the previous behavior was intentional, I was certainly unaware of it. We haven't offered backwards compatibility for any commands apart from those used for release builds. I don't think we should start now, especially over something so simple. We can fix their CI script.

Instead, let's focus on providing good semantics for this command. If choosing the main context is so obvious, could you spell out the criteria? The previous implementation in this PR was hardly obvious to me. The new implementation is to run the tests in every context, which I suppose is more sensible, but is still incomplete.

What happens when a test is defined in one context but not another? This can happen when a test has enabled_if on it. I guess that in your implementation, that contexts where the test is undefined should be skipped? I don't think it's working this way at the moment.

I argue that running the tests in every context is also bad user experience. The automatic building of everything in every context is a reason why contexts are mostly avoided in the first place. I would instead go for something like this:

  1. We can select an individual context with --context like we do for other commands.
  2. Absent of a selection, we choose the default context. If there's only one context defined, we can assume it's the default context.
  3. To run tests in all contexts, we can either defer to the build command, or introduce something else.

@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 27, 2026

Absent of a selection, we choose the default context. If there's only one context defined, we can assume it's the default context.

Right, this is the part that is missing here actually. The main context from oxcaml is the only context, therefore it is the "obvious choice" that I was mentioning earlier.

My previous idea of picking the first in the list would work in this case too, but would break down when using two non-default contexts. My thinking there was that "at this point, this is on the user", but that's exactly the kind of thinking that caused the issue in the first place.

I think outside of an obvious choice, we should just error. dune runtest already supports --context and building in multiple contexts by passing _build/default _build/other as the args.

Will this fix their CI? No. But at least they can rework it to be dune runtest --context main.

@Alizter Alizter marked this pull request as draft March 27, 2026 13:00
@Alizter Alizter marked this pull request as draft March 27, 2026 13:00
@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 27, 2026

@rgrinberg The fix is to now choose default, if that's not available then the member of the singleton set of contexts and that's not a singleton we error.

This changes the tested behaviour of dune runtest.

@Alizter Alizter changed the title fix(runtest): classify tests per-context fix(runtest): don't run in all contexts Mar 27, 2026
@Alizter Alizter force-pushed the push-ououwvzupysq branch from f41c1ec to 6678029 Compare March 27, 2026 13:09
@Alizter Alizter marked this pull request as ready for review March 27, 2026 13:12
@shonfeder
Copy link
Copy Markdown
Member

From the release perspective, if the decision is that the previous behavior was undefined and that this is not therefor a regression, then I don't think it belongs in the patch release. In particular, if this will not actually fix the build failures reported in #13904, then I don't think it needs to be in 3.22.1. The less that is added there the less risk, and the quicker we can actually fix the genuine regressions.

With the understanding that a software regression is "a type of software bug where a feature that has worked before stops working correctly" and that a fix for such a bug would be restoring the correct behavior of the feature, is this a fix for a regression?

@rgrinberg
Copy link
Copy Markdown
Member

rgrinberg commented Mar 29, 2026 via email

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes dune test/dune runtest context selection so that, for source-path test requests, Dune uses the default context when available, falls back to the single available context when there is exactly one, and errors out when multiple contexts exist without a default—avoiding the crash reported in #13904.

Changes:

  • Update runtest request construction to avoid assuming a "default" context exists, and to avoid running tests across all contexts for source paths.
  • Adjust blackbox tests to reflect the new “default-context-only” behavior for source-path invocations.
  • Add a changelog entry describing the behavior change and the crash fix.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
bin/runtest_common.ml Selects an appropriate context for source-path test requests (default → sole context → error).
test/blackbox-tests/test-cases/runtest/runtest-cmd.t Updates expected output for context selection and no-default-context workspace behavior.
test/blackbox-tests/test-cases/runtest/runtest-cmd-inline-tests.t Updates inline-test expectations so tests don’t run in multiple contexts by default.
doc/changes/fixed/13930.md Documents the behavior change and the crash fix in the changelog.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…wise

Signed-off-by: Ali Caglayan <alizter@gmail.com>
@Alizter Alizter force-pushed the push-ououwvzupysq branch from 6678029 to 17ae16a Compare March 29, 2026 20:02
@shonfeder shonfeder mentioned this pull request Mar 30, 2026
25 tasks
@Alizter Alizter merged commit 5fedf28 into ocaml:main Mar 30, 2026
78 of 81 checks passed
@Alizter Alizter deleted the push-ououwvzupysq branch March 30, 2026 08:45
@Alizter
Copy link
Copy Markdown
Collaborator Author

Alizter commented Mar 30, 2026

From the release perspective, if the decision is that the previous behavior was undefined and that this is not therefor a regression, then I don't think it belongs in the patch release. In particular, if this will not actually fix the build failures reported in #13904, then I don't think it needs to be in 3.22.1. The less that is added there the less risk, and the quicker we can actually fix the genuine regressions.

Please include this in 3.22.1. It's easier for us to fix dune than it is to communicate workarounds to users. I don't see the risk in including this.

@shonfeder
Copy link
Copy Markdown
Member

Since it in fact fixes the behavior and doesn't still require a workaround, I think it should indeed be included! My question was at a point in the discussion where that wasn't decided, IIUC. Thanks!

Leonidas-from-XIV pushed a commit to Leonidas-from-XIV/dune that referenced this pull request Mar 30, 2026
When choosing which context to find and run our tests, we always choose
the default context and if that is not available a unique context. When
there is no good choice we error.

- Fixes ocaml#13904
- [x] Changelog
Leonidas-from-XIV pushed a commit to Leonidas-from-XIV/dune that referenced this pull request Mar 30, 2026
When choosing which context to find and run our tests, we always choose
the default context and if that is not available a unique context. When
there is no good choice we error.

- Fixes ocaml#13904
- [x] Changelog

Signed-off-by: Marek Kubica <marek@tarides.com>
Leonidas-from-XIV added a commit that referenced this pull request Mar 30, 2026
Cherry-picket the commit and only changed the version of dune-lang to
3.22.

Signed-off-by: Marek Kubica <marek@tarides.com>
Co-authored-by: Ali Caglayan <alizter@gmail.com>
shonfeder added a commit to shonfeder/opam-repository that referenced this pull request Apr 1, 2026
CHANGES:

### Fixed

- Restore compatibility with Windows 7 (ocaml/dune#13905, @nojb)

- `dune test` now runs tests in the default context only. When there is a
  single context, it is treated as the default. This fixes a crash when the
  workspace has no context named "default".
  (ocaml/dune#13930, fixes ocaml/dune#13904, @Alizter)

- Fix `dune trace cat --chrome-trace` to adhere to the Chrome Trace Event
  Format by providing timestamps and durations at microsecond granularity
  (ocaml/dune#13911, fixes ocaml/dune#13906, @Alizter)
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.

3.22.0 crash due to an internal error

5 participants