playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG]: --last-failed silently drops all tests when using project dependencies

Open ariel42 opened this issue 8 months ago • 5 comments

Bug description

--last-failed silently results in "No tests found" when the config uses project dependencies. Tests from dependency projects are never checked against the --last-failed filter and are excluded entirely.

Reproduction

playwright.config.ts:

export default defineConfig({
    testDir: '.',
    projects: [
        {
            name: 'default',
            testIgnore: /setup/,
        },
        {
            name: 'sequential',
            testMatch: /setup/,
            dependencies: ['default'],
        },
    ],
});

Steps:

  1. Run tests — some in the default project fail
  2. Run npx playwright test --last-failed
  3. Result: Error: No tests found — even though .last-run.json contains valid failed test IDs

Root cause

In runner/loadUtils.js, the postShardTestFilters (used by --last-failed) is applied on line 173-174, but dependency projects are only added to rootSuite later on line 179-180:

// Line 145-151: Only "top-level" projects are added to rootSuite
const projectClosure = buildProjectsClosure([...filteredProjectSuites.keys()], ...);
for (const [project, type] of projectClosure) {
    if (type === "top-level") {
        rootSuite._addSuite(buildProjectSuite(project, ...));
    }
}

// Line 173-174: postShardTestFilters runs — but rootSuite only has top-level projects
if (config.postShardTestFilters.length)
    filterTestsRemoveEmptySuites(rootSuite, (test) => config.postShardTestFilters.every((filter) => filter(test)));

// Line 177-183: Dependency projects are added AFTER the filter already ran
const projectClosure2 = new Map(buildProjectsClosure(rootSuite.suites.map(...)));
for (const [project, level] of projectClosure2.entries()) {
    if (level === "dependency")
        rootSuite._prependSuite(buildProjectSuite(project, projectSuites.get(project)));
}

When default is a dependency of sequential:

  1. Only sequential tests are in rootSuite when postShardTestFilters runs
  2. The failed test IDs (from default project) don't match any sequential test IDs
  3. All sequential tests are filtered out → "No tests found"
  4. default project tests are added afterward but were never filtered — they just get added in full (but since all top-level tests were removed, the suite is empty)

Confirmed via instrumentation

Added debug logging to LastRunReporter.filterLastFailed():

[DEBUG] failedTestIds: ["44ee58bcb63fdfc96a27-8cbdbefff5ceb59d5a64", ...]
[DEBUG] no match: test.id=c540d76cbc29c39320a1-39d5ca1a35c64343e96a title=Toggle visible in header

The 44ee58bc... prefix = default project's file hash. The c540d76c... prefix = sequential project's file hash. Only sequential tests are checked; default tests are never seen by the filter.

Expected behavior

--last-failed should filter tests from ALL projects (including dependencies), not just top-level projects.

Suggested fix

Move the postShardTestFilters application to after dependency projects are added to rootSuite, or apply the filter to all project suites before the closure is built.

Version

  • Playwright: 1.58.2
  • OS: Windows 10
  • Node.js: 24.14.0

ariel42 avatar Mar 22 '26 22:03 ariel42

I can take this if the team still wants it handled as a community contribution.

I noticed the earlier PR for this issue was closed because it was opened before maintainer approval/assignment, so I will not open another PR unless this is assigned or explicitly approved.

The narrow plan I would follow:

  • add a runner regression where --last-failed selects a failed test from a dependency project
  • keep dependency-project inclusion scoped to the last-failed filter path
  • avoid changing normal dependency ordering or shard behavior outside that case

Happy to work through it if that direction matches what maintainers want.

mturac avatar Jun 16 '26 22:06 mturac

@mturac Thank you for your interest in this one.

I am not sure what the right behavior should be. So far, playwright maintains invariant that all dependencies are running whenever a top-level test runs. If we apply --last-failed filter to the dependency project, we'll break the all part of the invariant.

It seems like we should have a better strategy here, and perhaps find the set of "last-failed" tests that are not a (possibly indirect) dependency of some other "last-failed" test, and filter to those ones. That will automatically include all dependencies after. That said, we probably need to filter projects by "last-failed" at the start, so that "default" project from the example above becomes a top-level project rather than a dependency.

Overall, this is not that straightfoward. I'd recommend to come up with some test scenarios where various subsets of dep/top-level tests fail and understand the desired behavior. Only then we can proceed to the implementation.

dgozman avatar Jun 17 '26 12:06 dgozman

Thanks, that makes sense. I agree that the simple “apply --last-failed to dependency projects too” shape is not right if it means dependency projects stop running all of their tests when they are pulled in by a selected top-level project.

I’ll step back and start with behavior scenarios before touching the implementation. The matrix I’m going to validate is roughly:

  1. Only a top-level project test failed

    • app depends on setup
    • last failed: app/test-1
    • expected: run all setup tests, then only app/test-1
  2. Only a dependency project test failed

    • app depends on setup
    • last failed: setup/test-1
    • expected: treat setup as the selected top-level project for this run, so setup/test-1 can run instead of producing “No tests found”
  3. Both dependency and top-level tests failed

    • app depends on setup
    • last failed: setup/test-1, app/test-1
    • expected: select app/test-1 as the top-level failure and include all setup tests as dependencies; do not separately filter setup down to setup/test-1
  4. Transitive dependencies

    • app depends on seed, seed depends on setup
    • last failed only in seed: run all setup tests, then the failed seed tests
    • last failed in setup and app: run all setup/seed dependency tests, then the failed app tests
  5. Multiple selected top-level projects sharing a dependency

    • chromium and firefox both depend on setup
    • last failed: one test in each browser project
    • expected: run setup once as an unfiltered dependency, then the failed browser tests

So the rule I’ll try to encode in tests first is: choose the last-failed project/test entries that are not already covered by another selected last-failed project through the dependency graph; then let the normal dependency closure add dependencies unfiltered. If a failed test is only in what is normally a dependency project, that project can become a selected top-level project for this --last-failed run.

If this matches your expectations, I’ll turn these into focused regression tests first and only then look at the implementation shape.

mturac avatar Jun 17 '26 13:06 mturac

@mturac Sounds like a good plan. I agree with the disposition on all the cases.

dgozman avatar Jun 19 '26 12:06 dgozman

I would challenge this disposition and try to make things simpler. I would document --last-failed as only working for the top-level project.

pavelfeldman avatar Jun 22 '26 23:06 pavelfeldman