Skip to content

[rust] Find the cached Grid jar in the offline fallback - #18392

Open
fuleinist wants to merge 12 commits into
SeleniumHQ:trunkfrom
fuleinist:fix/grid-cache-jar-fallback
Open

fuleinist wants to merge 12 commits into
SeleniumHQ:trunkfrom
fuleinist:fix/grid-cache-jar-fallback

Conversation

@fuleinist

@fuleinist fuleinist commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

selenium-manager --grid with no version falls back to the newest jar in the cache. That lookup fails on Linux and Windows, even when the jar is on disk.

find_best_driver_from_cache matches cache files with the shared is_driver matcher. That matcher expects the driver name and the binary extension. For Grid, those are selenium-server on Linux and selenium-server.exe on Windows. The cached file is selenium-server-<version>.jar, so the suffix match finds no file. macOS uses a contains match, so the same lookup succeeds there.

Fix

Two changes in GridManager:

  • is_driver now accepts a regular file whose name starts with selenium-server- and ends with .jar.
  • find_best_driver_from_cache ranks the cached jars by the version in the parent directory. It accepts a jar only when the version in the file name equals the version of that directory. It scans only the Grid cache directory.

The fallback rules are:

  • The fallback runs only when the request names no version.
  • A pinned version, nightly or latest never uses a cached jar. An exact cached version still resolves through the normal cache path.
  • A jar is ranked only when the version in its file name equals the version of its parent directory. This skips a snapshot jar inside a stable-named directory, and a jar whose name and directory disagree.
  • The scan starts at the Grid cache directory. A matching jar in another cache directory is never returned.
  • The legacy lookup covers only a jar outside a version-named directory. It keeps its snapshot file name filter. A jar inside a version-named directory is handled by the ranked pass alone.

The CLI applies --driver-version after the manager is built. The CLI path now recomputes the fallback gate after it sets the version, so --grid --driver-version 4.10.0 cannot return a different cached jar.

The shared matcher and find_latest_from_cache keep their current behaviour, so no browser driver changes.

Tests

The tests in rust/tests/grid_tests.rs build a cache directory and call find_best_driver_from_cache, or run the binary against a temporary --cache-path:

  • grid_cache_fallback_finds_cached_jar_test finds grid/4.54.0/selenium-server-4.54.0.jar. This test fails on Windows and Linux without the change.
  • grid_cache_fallback_ignores_other_files_test ignores selenium-server and selenium-server.zip in the same directory.
  • grid_cache_fallback_prefers_newest_version_test caches 4.9.0 and 4.54.0, and expects 4.54.0.
  • grid_cache_fallback_skips_snapshot_test caches 4.54.0, 4.54.0-SNAPSHOT and 4.55.0-SNAPSHOT, and expects the 4.54.0 jar.
  • grid_cache_fallback_skips_snapshot_file_outside_version_dir_test puts a snapshot jar beside the version directories, and expects no result.
  • grid_cache_fallback_skips_snapshot_jar_in_stable_dir_test puts a snapshot jar inside grid/4.54.0, and expects no result.
  • grid_cache_fallback_requires_jar_name_to_match_dir_test puts selenium-server-4.9.0.jar in grid/4.54.0 beside a correct 4.53.0 jar, and expects the 4.53.0 jar.
  • grid_cache_fallback_ignores_mismatched_jar_in_version_dir_test puts only grid/4.54.0/selenium-server-4.9.0.jar in the cache, and expects no result.
  • grid_cache_fallback_ignores_jar_in_other_cache_dir_test puts other/4.54.0/selenium-server-4.54.0.jar in the cache root with no Grid jar, and expects no result.
  • grid_cache_fallback_cli_offline_returns_cached_jar_test runs --grid --offline against a temporary cache, and expects that jar.
  • grid_cache_fallback_cli_pinned_version_ignores_cache_test runs --grid 4.10.0 --offline with only 4.54.0 cached, and expects an empty driver_path.
  • grid_cache_fallback_cli_driver_version_ignores_cache_test runs --grid --driver-version 4.10.0 --offline with only 4.54.0 cached, and expects an empty driver_path.
  • grid_error_test runs against a warm cache that holds a jar, and still expects DATAERR.

The tests need no network access and no Java, except grid_error_test, which resolves a version online.

Local results

On Windows 11 with Rust 1.99.1:

  • cargo fmt -- --check exits 0.
  • cargo test --test grid_tests grid_cache_fallback passes 12 tests.
  • cargo test --test grid_tests grid_error_test passes 2 tests.
  • cargo test --lib passes 46 tests.
  • cargo test --test cache_unit_tests passes 14 tests.
  • Each new test fails when its source change is stashed. The file-name guard gives 8 passed and 2 failed. The Grid-subtree scope gives 10 passed and 1 failed. The legacy version-directory rule gives 11 passed and 1 failed. Each new CLI guard fails when its source change is reverted, so the tests detect the old behaviour.
  • cargo clippy --all-targets -- -D warnings reports lints that predate this branch in untouched files. This branch adds none in the changed lines.

Manual check

I built the binary and ran it against a cache that holds only the jar:

selenium-manager --grid --offline --cache-path <tmp> --output json

The result reports driver_path as <tmp>/grid/4.54.0/selenium-server-4.54.0.jar and exits 0.

Fixes #18391

The fallback for `--grid` without a version calls
`find_best_driver_from_cache`, and that lookup matches cache entries with the
shared `is_driver` matcher in `rust/src/lib.rs`. The matcher looks for the
driver name plus the binary extension: `selenium-server` on Linux and
`selenium-server.exe` on Windows. The cached artifact is
`selenium-server-<version>.jar`, so the suffix match finds nothing on either
platform. macOS uses a `contains` match, which is why the lookup works there.

`GridManager` now overrides `is_driver` and accepts a file whose name starts
with `selenium-server-` and ends with `.jar`. The shared matcher that every
browser driver uses keeps its behaviour.

Fixes SeleniumHQ#18391
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@CLAassistant

CLAassistant commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@selenium-ci selenium-ci added C-rust Rust code is mostly Selenium Manager B-manager Selenium Manager labels Nov 26, 2026
@titusfortner titusfortner added the Q-qodo-review Request a Qodo review label Nov 27, 2026
@selenium-ci selenium-ci removed the Q-qodo-review Request a Qodo review label Nov 27, 2026
@qodo-code-review

qodo-code-review Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (4) 📘 Rule violations (2) 📜 Skill insights (0)

Grey Divider


Action required

1. Nightly fallback can run a stable Grid jar 🐞 Bug ≡ Correctness
Description
find_best_driver_from_cache checks whether the current driver version is nightly, but setup
replaces that value with a concrete -SNAPSHOT version after successful discovery. If downloading
the discovered jar then fails, a cache containing an older snapshot and a numerically newer stable
jar returns the stable jar for the original nightly request.
Code

rust/src/grid.rs[327]

+        let want_snapshot = self.is_nightly(self.get_driver_version());
Evidence
Grid nightly discovery produces a concrete snapshot version, which setup stores in place of
nightly. A later download error reaches the CLI cache fallback; is_nightly recognizes nightly
and canary, not the stored snapshot version, so the new snapshot preference is bypassed and
numeric ranking selects the newer stable jar.

rust/src/grid.rs[121-159]
rust/src/lib.rs[917-923]
rust/src/lib.rs[822-825]
rust/src/lib.rs[974-989]
rust/src/main.rs[318-342]
rust/src/grid.rs[324-366]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
After a nightly request resolves to a concrete `-SNAPSHOT` version, a download failure can make cache fallback select a newer stable jar instead of an available snapshot.
## Fix Focus Areas
- rust/src/grid.rs[324-361]
- rust/tests/grid_tests.rs[244-288]
## Recommended Fix
Treat a resolved `-SNAPSHOT` driver version as a snapshot request when selecting the cached fallback, while retaining support for the original `nightly` value. Add a regression test with a resolved snapshot version, an older cached snapshot, and a numerically newer cached stable jar.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


2. Offline nightly Grid runs a stable jar ✓ Resolved Bug Correctness
Description
version_from_dir_name rejects the SNAPSHOT suffix in a cached nightly jar’s version directory,
and find_best_driver_from_cache skips that jar whenever it finds a numeric-versioned one. If
nightly version discovery fails offline with both a newer nightly jar and an older stable jar
cached, the fallback returns the stable jar without reaching its nonnumeric fallback.


Remediation recommended

3. Grid test file fails the repository formatter check 📘 Rule violation ⚙ Maintainability
Description
The fs::write call in grid_cache_fallback_requires_jar_name_to_match_dir_test is split across
lines, but its arguments (mismatched_dir.join("selenium-server-4.9.0.jar"), b"fake jar") are 57
characters, under rustfmt's 60-character argument-width threshold, so rustfmt collapses it to one
line. The same-width call in grid_cache_fallback_ignores_jar_in_other_cache_dir_test
(fs::write(other_dir.join("selenium-server-4.54.0.jar"), b"fake jar").unwrap();) is already on one
line, so the two sites disagree and the split one is the one rustfmt would change. This fails the
cargo fmt -- --check gate the PR description says it passed.
Code

rust/tests/grid_tests.rs[R366-370]

+    fs::write(
+        mismatched_dir.join("selenium-server-4.9.0.jar"),
+        b"fake jar",
+    )
+    .unwrap();
Evidence
The split fs::write call in the new test is inconsistent with the single-line fs::write call of
identical argument width elsewhere in the same new test block, so the formatter would rewrite this
hunk.

rust/tests/grid_tests.rs[366-370]
rust/tests/grid_tests.rs[392-392]
AGENTS.md: Ensure Changed Code Passes Repository Formatting: AGENTS.md: Ensure Changed Code Passes Repository Formatting: AGENTS.md: Ensure Changed Code Passes Repository Formatting: AGENTS.md: Ensure Changed Code Passes Repository Formatting

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
Issue description
The `fs::write` call in the test `grid_cache_fallback_requires_jar_name_to_match_dir_test` in `rust/tests/grid_tests.rs` is split over several lines, but rustfmt keeps it on one line because its arguments are under the 60-character threshold. This breaks the repository formatter check.
Fix Focus Areas
- rust/tests/grid_tests.rs[366-370]
Recommended Fix
Replace the multi-line call with `fs::write(mismatched_dir.join("selenium-server-4.9.0.jar"), b"fake jar").unwrap();` on one line, matching the style of the `fs::write(other_dir.join(...))` call in `grid_cache_fallback_ignores_jar_in_other_cache_dir_test`. Then run `cargo fmt -- --check` to confirm.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


4. Offline Grid can return a mismatched jar 🐞 Bug ≡ Correctness
Description
find_best_driver_from_cache rejects a jar whose filename version disagrees with its parent
directory during ranking, but the legacy lookup does not enforce that check. If the cache contains
only grid/4.54.0/selenium-server-4.9.0.jar, the legacy lookup returns that rejected jar and the
offline CLI reports it as a successful fallback.
Code

rust/src/grid.rs[365]

+                && !entry.path().parent().is_some_and(is_snapshot_dir)
Evidence
The ranked pass rejects mismatched versions, but the recursive legacy lookup applies only snapshot
filters and returns its last match. The CLI uses that result as its driver path on a fallback.

rust/src/grid.rs[337-345]
rust/src/grid.rs[361-370]
rust/src/files.rs[684-708]
rust/src/main.rs[327-346]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The legacy fallback accepts jars in version-named directories even when the jar version disagrees with the directory version, undoing the ranked lookup's validation.
## Fix Focus Areas
- rust/src/grid.rs[361-370]
- rust/tests/grid_tests.rs[360-384]
## Recommended Fix
Allow the legacy lookup only for jars outside version-named directories, while preserving the snapshot exclusions. Add a test with a mismatched jar as the only cached candidate and assert that no jar is returned.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


5. Grid can use another cache's server jar 🐞 Bug ≡ Correctness
Description
find_best_driver_from_cache collects candidates from the entire cache root, while its ranked
checks validate only the jar’s immediate parent and filename. With
other/4.54.0/selenium-server-4.54.0.jar in that root, an unversioned Grid request can report that
unrelated path as its driver when setup falls back.
Code

rust/src/grid.rs[R328-329]

+        let cache_path = self.get_cache_path()?.unwrap_or_default();
+        let jars = collect_files_from_cache(&cache_path, |entry| self.is_driver(entry));
Evidence
Grid's exact-version path is under grid/, but the new fallback scans the cache root recursively
and checks no Grid ancestor. The CLI reports the resulting fallback path when setup fails.

rust/src/grid.rs[207-223]
rust/src/grid.rs[311-339]
rust/src/grid.rs[353-366]
rust/src/files.rs[684-695]
rust/src/main.rs[325-348]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new Grid cache lookup can select a matching jar from a non-Grid subtree of the configured cache.
## Fix Focus Areas
- rust/src/grid.rs[327-367]
- rust/tests/grid_tests.rs[118-196]
## Recommended Fix
Scope both ranked and legacy scans to the `grid` cache subtree, preserving the supported legacy layout within that subtree. Add a test with a matching jar under another cache directory and no Grid jar.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


View review recommended (2)
6. Offline Grid can select a snapshot jar 🐞 Bug ≡ Correctness
Description
find_best_driver_from_cache ranks jars by their numeric parent directory without checking the
filename for SNAPSHOT. If a snapshot jar sits in a stable-version directory, the ranked path
returns it before the legacy lookup's snapshot filename filter runs.
Code

rust/src/grid.rs[R331-334]

+        for jar in jars {
+            let Some(version) = jar.parent().and_then(version_from_dir_name) else {
+                continue;
+            };
Evidence
A file named grid/4.54.0/selenium-server-4.55.0-SNAPSHOT.jar passes the new jar matcher. Its
parent parses as a stable numeric version, so the ranked branch can return it; only the subsequent
legacy lookup checks filenames for SNAPSHOT. The added snapshot tests place snapshots in
snapshot-named directories or directly under grid/, leaving this path untested.

rust/src/grid.rs[311-320]
rust/src/grid.rs[326-359]
rust/tests/grid_tests.rs[208-229]
rust/tests/grid_tests.rs[287-307]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The ranked Grid cache lookup can return a snapshot jar from a numeric version directory because it checks only the directory name.
## Fix Focus Areas
- rust/src/grid.rs[311-359]
- rust/tests/grid_tests.rs[208-229]
## Recommended Fix
Exclude snapshot filenames from the ranked candidates, and verify that a jar in a numeric version directory names the same version as that directory. Add a regression test with a snapshot jar inside a stable-version directory.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


7. Offline Grid fallback can pick an older cached jar ✓ Resolved Bug Correctness
Description
find_best_driver_from_cache falls back to find_latest_from_cache, which takes the last entry of
collect_files_from_cache after WalkDir::sort_by_file_name(), so ordering is lexicographic by
directory and file name. With cached grid/4.9.0/selenium-server-4.9.0.jar and
grid/4.54.0/selenium-server-4.54.0.jar, the string 4.9.0 sorts after 4.54.0, so the older
4.9.0 jar is returned as the 'newest'. This happens in the offline --grid fallback whenever more
than one Grid version is cached, and the new is_driver override is what makes the lookup reach
this sort at all on Linux and Windows.


Informational

8. A legacy-pass comment restates the code it sits on 📘 Rule violation ⚙ Maintainability
Description
The comment The legacy pass covers only jars outside a version-named directory. sits above the
.and_then(version_from_dir_name).is_none() predicate and only paraphrases that check. It gives no
rationale for why the legacy pass must exclude version-named directories, which is the detail a
later reader would need before changing the filter.
Code

rust/src/grid.rs[365]

+                // The legacy pass covers only jars outside a version-named directory.
Evidence
The comment describes the predicate directly beneath it without saying why the ranked pass must own
version-named directories.

rust/src/grid.rs[361-370]
AGENTS.md: Write Comments That Explain Why Rather Than What: AGENTS.md: Write Comments That Explain Why Rather Than What: AGENTS.md: Write Comments That Explain Why Rather Than What: AGENTS.md: Write Comments That Explain Why Rather Than What

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
Issue description
The comment above the legacy-pass predicate in `GridManager::find_best_driver_from_cache` in `rust/src/grid.rs` only restates the `version_from_dir_name(...).is_none()` check. It does not explain why version-named directories are excluded from the legacy pass, so a later reader could remove the exclusion without understanding the risk.
Fix Focus Areas
- rust/src/grid.rs[361-370]
Recommended Fix
Replace the comment with one that states the reason, for example: the ranked pass already validates jars in version-named directories against their file-name version, so the legacy pass must skip them to avoid returning an unvalidated jar from the same directory.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


Grey Divider

Context sources
✅ Context preparation
  Commit messages: summarized; full text retained for this review
✅ REVIEW.md
Review mode: Auto: ⚖️ Balanced: Downgraded extended -> standard: change is below the extended eligibility bar (hunks 7/18, lines 463/200; both must reach the floor). Router rationale: Multiple subtle cache, version, fallback, and CLI paths warrant redundant review.

Grey Divider

⚠️ Warning | Comment size exceeded, non finding content trimmed Learn more

Grey Divider

Qodo Logo

Comment thread rust/src/grid.rs Resolved
find_latest_from_cache returns the last file of a WalkDir walk sorted by
file name, so the comparison is lexicographic. With grid/4.9.0 and
grid/4.54.0 both cached, the string 4.9.0 sorts after 4.54.0, so the
older jar wins.

GridManager now overrides find_best_driver_from_cache. It collects the
cached jars and compares the version in each parent directory as numbers.
The shared lookup keeps its behaviour, and it still handles a jar outside
a version-named directory.

The new test caches 4.9.0 and 4.54.0 and asserts that 4.54.0 is
returned. Fixes the review finding on SeleniumHQ#18392.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@fuleinist

Copy link
Copy Markdown
Author

I pushed 1023cda to answer the review finding.

Change. GridManager overrides find_best_driver_from_cache. It collects the cached jars and picks the one whose parent directory holds the highest version, compared as numbers. The shared lookup is unchanged for browser drivers, and it still handles a jar outside a version-named directory.

Test. grid_cache_fallback_prefers_newest_version_test caches grid/4.9.0/selenium-server-4.9.0.jar and grid/4.54.0/selenium-server-4.54.0.jar and asserts that 4.54.0 is returned. Without the change it fails and returns the older jar.

Gates on 1023cda (Windows 11, Rust 1.99.1):

  • cargo fmt --check exits 0.
  • cargo test --test grid_tests grid_cache passes 3 tests.
  • cargo test --lib passes 46 tests.
  • cargo test --test cache_unit_tests passes 14 tests.

The branch is ready for another review on 1023cda.

@titusfortner titusfortner added the Q-qodo-review Request a Qodo review label Nov 28, 2026
@selenium-ci selenium-ci removed the Q-qodo-review Request a Qodo review label Nov 28, 2026
Comment thread rust/src/grid.rs Resolved
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 1023cda

@titusfortner

Copy link
Copy Markdown
Member

One gap: the fallback also runs when a version is requested, so --grid 4.10.0 --offline silently returns a different cached jar. Can you disable fallback_driver_from_cache in GridManager::new when a version is given, and tighten grid_error_test to require DATAERR?

version_from_dir_name parsed every dot-separated part as a number, so a 4.55.0-SNAPSHOT directory returned nothing and its jar was skipped. An offline nightly request then ran a stable jar from the cache.

The directory name now yields the numeric parts plus a snapshot flag. A snapshot outranks a stable jar of the same numeric version. When the requested version is nightly, the newest cached snapshot wins over a newer stable jar.

Four regression tests cover the ranking. Three of them fail on the previous commit.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@titusfortner titusfortner added the Q-qodo-review Request a Qodo review label Nov 28, 2026
@selenium-ci

Copy link
Copy Markdown
Member

/agentic_review

@selenium-ci selenium-ci removed the Q-qodo-review Request a Qodo review label Nov 28, 2026
Comment thread rust/src/grid.rs Resolved
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit ca5ea7d

A pinned Grid version now disables the cache fallback. An offline
request for a version that is not cached returns DATAERR. It no longer
returns a different cached jar. An empty version, "nightly", "canary"
and "latest" name a moving target. These keep the fallback.

The fallback also treats a resolved "-SNAPSHOT" version as a nightly
request. Setup resolves a nightly request to a concrete snapshot
version. If that download then fails, the fallback prefers a cached
snapshot over a newer stable jar.

Tighten grid_error_test to require the DATAERR exit code. Add a
regression test for the resolved nightly fallback.
@fuleinist

Copy link
Copy Markdown
Author

Addressed both review findings in 3d50151.

A pinned version no longer uses the cache fallback (@titusfortner)

GridManager::new now disables fallback_driver_from_cache when the request names a concrete version. An empty version, nightly, canary and latest name a moving target, so they keep the fallback. A pinned version that is not cached now returns DATAERR instead of a different cached jar.

I tightened grid_error_test to require the DATAERR exit code. The old form used try_success plus assert_output. That form also passed when the fallback returned a wrong jar, because the fallback log line contains the text "There was an error".

A resolved nightly version keeps its snapshot (qodo finding)

find_best_driver_from_cache now treats a resolved -SNAPSHOT version as a nightly request. setup replaces nightly with a concrete -SNAPSHOT version after discovery. If that download then fails, the fallback prefers a cached snapshot over a numerically newer stable jar.

Local results

  • cargo test --test grid_tests grid_cache_fallback: 8 passed. This includes the new grid_cache_fallback_resolved_nightly_prefers_snapshot_test.
  • cargo test --test grid_tests grid_error_test: 2 passed.
  • cargo test --lib: 46 passed.
  • cargo test --test cache_unit_tests: 14 passed.
  • cargo fmt --check: clean.

The new test fails without the snapshot fix, so it covers the finding.

@titusfortner Could you review this again when you have a moment? Thanks.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@bonigarcia bonigarcia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, the is_driver fix and the numeric ordering look right. A few changes before merging:

  1. (Medium) A plain --grid can return a nightly. best ranks snapshots with stable jars, so with 4.54.0 and 4.55.0-SNAPSHOT cached, the fallback returns the snapshot. Online resolution excludes snapshots. The tie rule is also inverted: 4.54.0-SNAPSHOT precedes 4.54.0. Please skip snapshots, including in the final find_latest_from_cache call, and replace the two tests that expect the snapshot to win with one that expects the stable jar.
  2. (Medium) I'd only fall back when no version is given. latest isn't a supported Grid version: setup() never resolves it, so this turns an error into success with an arbitrary cached jar. A nightly request can also return a stable jar, which isn't what was asked for. config.fallback_driver_from_cache = driver_version.is_empty() fixes both, stays within the scope of #18391, and lets you drop want_snapshot, best_snapshot and the three nightly-request tests. An unexpired metadata entry or an exact x.y.z-SNAPSHOT version still finds a cached nightly. One trade-off: on macOS the fallback currently works for nightly, so that would change. @titusfortner, does that match what you had in mind?
  3. (Medium) Please test through the CLI. With a fake jar in a temp --cache-path (no network needed): --grid --offline returns the jar, and --grid 4.10.0 --offline with only 4.54.0 cached returns an empty driver_path. Please also give grid_error_test a temp cache that already holds a jar; with a cold cache it passes without the fix.
  4. (Low) An uncached pinned version doesn't return DATAERR with --offline; it exits OK with an empty driver_path. Ruby relies on that, so no code change is needed.
  5. (Low) --grid --driver-version 4.10.0 bypasses the check, since the version is set after new().

The offline fallback applies only when the request names no version,
because a named version must resolve to that exact jar. An empty
version is the only case that can use a cached jar. A request for
nightly or latest no longer falls back, so the resolved snapshot case
is gone.

The fallback now skips SNAPSHOT directories in the version ranking and
in the shared cache lookup. A plain request can no longer run a cached
nightly build.

Replace the nightly and snapshot ranking tests with one stable jar
test. Add two CLI tests that use a temporary cache. Run
grid_error_test against a warm cache.
@fuleinist

fuleinist commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thanks. I pushed 9f50eae to address the three middle items.

1. Snapshots are skipped. The cache fallback now parses only stable version directories. It skips any directory name that ends with SNAPSHOT, in both the version ranking and the final find_latest_from_cache call. grid_cache_fallback_skips_snapshot_test caches 4.54.0, 4.54.0-SNAPSHOT and 4.55.0-SNAPSHOT. It asserts that the 4.54.0 jar wins. The two tests that expected a snapshot to win are removed. That test fails on the previous head, so it catches the old behaviour.

2. The fallback needs an empty version. GridManager::new now sets config.fallback_driver_from_cache = driver_version.is_empty(). want_snapshot, best_snapshot and the three nightly-request tests are removed. A nightly or latest request no longer uses the fallback, on any platform.

3. CLI coverage. grid_cache_fallback_cli_offline_returns_cached_jar_test puts a fake jar in a temporary --cache-path. It checks that --grid --offline returns that jar. grid_cache_fallback_cli_pinned_version_ignores_cache_test checks that --grid 4.10.0 --offline returns an empty driver_path when only 4.54.0 is cached. grid_error_test now runs against a warm cache that holds a jar.

4. No code change. I confirmed that an uncached pinned version exits OK with an empty driver_path under --offline.

5. Known gap. --grid --driver-version 4.10.0 still bypasses the check. The CLI applies the version after new(), so the manager cannot see it. I left it out to keep this PR inside the scope of #18391. I can open a follow-up issue if you want that fixed separately.

Gates on 9f50eae: cargo fmt -- --check is clean. All 6 cache-fallback tests pass. Both grid_error_test cases pass. cargo clippy --all-targets -- -D warnings still reports lints that predate this branch. They sit in other files, plus src/grid.rs:124 and :202. This branch does not change those lines.

Could you take another look when you have time?

The offline cache fallback for Grid now runs only when no version is
requested. Snapshot jars are skipped both in version-directory ranking
and in the legacy lookup for jars outside a version-named directory,
checking both the parent directory name and the jar file name.

When --driver-version is supplied with --grid on the CLI, recompute the
cache-fallback gate after applying the driver version so an explicit CLI
driver version clears the gate instead of falling back to a cached jar.
@fuleinist

Copy link
Copy Markdown
Author

Follow-up on item 5 in the review. It is fixed in 0ac3bc6, on top of 9f50eae.

--grid --driver-version 4.10.0 no longer uses the cache fallback

main.rs applies --driver-version after GridManager::new, so the flag set in new() could not see it. The CLI path now recomputes fallback_driver_from_cache from the effective driver version, for Grid only. An explicit CLI driver version clears the gate.

grid_cache_fallback_cli_driver_version_ignores_cache_test covers it. It puts 4.54.0 in a temporary cache, runs --grid --driver-version 4.10.0 --offline, and asserts that driver_path is empty. The test fails when the main.rs change is reverted.

I also closed one edge case in the same pass. A jar can sit outside a version-named directory. The final find_latest_from_cache call now skips a snapshot jar by file name as well as by directory name. grid_cache_fallback_skips_snapshot_file_outside_version_dir_test covers it, and it fails without the file-name check.

Item 4 needs no code change, as you wrote.

Gates on 0ac3bc6: cargo fmt -- --check exits 0. cargo test --test grid_tests grid_cache_fallback passes 8 tests. cargo test --test grid_tests grid_error_test passes 2 tests. cargo test --lib passes 46 tests. cargo test --test cache_unit_tests passes 14 tests.

The PR body now describes the current rules and the current test list.

Could you take another look when you have time?

@bonigarcia

Copy link
Copy Markdown
Member

Thanks, all five points are addressed, and fixing the --driver-version case too is appreciated.

One small thing: in main.rs, if grid.is_some() checks the flag rather than the selected manager. --browser/--driver (or SE_BROWSER) take precedence over --grid, so --browser chrome --driver-version X --grid would disable the Chrome fallback. if selenium_manager.is_grid() keeps it Grid-only. I'll approve once that's in and CI is green.

--browser and --driver take precedence over --grid when main selects a manager. The fallback gate now asks the manager, so a browser request keeps its cache fallback when --grid is also present.
@fuleinist

fuleinist commented Dec 1, 2026 •

Copy link
Copy Markdown
Author

Pushed 395338b.

Change. main.rs now gates the block on selenium_manager.is_grid(), not grid.is_some(). A browser or driver request keeps its cache fallback, even when --grid is also present. A Grid request keeps the existing version gate.

Gates. cargo check is clean. All eight grid_cache_fallback tests pass, including the CLI driver-version case.

The CI and CI - RBE workflows show action_required, so a maintainer must approve the fork run before they start.

Comment thread rust/src/grid.rs
Comment on lines +331 to +334
for jar in jars {
let Some(version) = jar.parent().and_then(version_from_dir_name) else {
continue;
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

3. Offline grid can select a snapshot jar 🐞 Bug ≡ Correctness

find_best_driver_from_cache ranks jars by their numeric parent directory without checking the
filename for SNAPSHOT. If a snapshot jar sits in a stable-version directory, the ranked path
returns it before the legacy lookup's snapshot filename filter runs.
Agent Prompt
## Issue description
The ranked Grid cache lookup can return a snapshot jar from a numeric version directory because it checks only the directory name.

## Fix Focus Areas
- rust/src/grid.rs[311-359]
- rust/tests/grid_tests.rs[208-229]

## Recommended Fix
Exclude snapshot filenames from the ranked candidates, and verify that a jar in a numeric version directory names the same version as that directory. Add a regression test with a snapshot jar inside a stable-version directory.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

@fuleinist fuleinist Dec 1, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 36a6c04.

You are right. The ranked loop trusted the parent directory name. It never checked the jar file name, so a snapshot jar inside a stable-named directory could win. The legacy lookup already rejected that file name.

find_best_driver_from_cache now parses the version from the jar file name and requires it to equal the version in the parent directory. A snapshot file name does not parse as a numeric version, so the same guard skips it. A mismatched jar is also skipped.

grid_cache_fallback_skips_snapshot_jar_in_stable_dir_test caches grid/4.54.0/selenium-server-4.55.0-SNAPSHOT.jar and expects no result. grid_cache_fallback_requires_jar_name_to_match_dir_test caches a mismatched 4.54.0 entry beside a correct 4.53.0 entry, and expects the 4.53.0 jar.

Probe: with the guard stashed, cargo test --test grid_tests grid_cache_fallback reports 8 passed and 2 failed. With the guard, it reports 10 passed.

The name is safe to trust because get_driver_path_in_cache writes {GRID_RELEASE}-{driver_version}.{GRID_EXTENSION} inside a {driver_version} directory.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 395338b

Require the version parsed from the cached Grid jar file name to equal
the numeric version parsed from its parent directory during ranked lookup.
This rejects snapshot jars placed in stable directories and mismatched jars
whose file name disagrees with their parent directory.
@fuleinist

fuleinist commented Dec 1, 2026 •

Copy link
Copy Markdown
Author

Pushed four follow-ups to Qodo findings: 36a6c04, b42da79, 898e12d and f970aed.

36a6c04. find_best_driver_from_cache accepts a ranked jar only when the version in its file name equals the version of its parent directory. This skips a snapshot jar inside a stable-named directory, and a jar whose name and directory disagree.

b42da79. Both scans start at the Grid cache directory, not the cache root. A matching jar in another cache directory is never returned.

898e12d. The legacy lookup covers only a jar outside a version-named directory. A jar inside one is handled by the ranked pass alone, so a mismatched jar cannot come back through the legacy path.

f970aed. Comment text only. It states why the legacy pass skips version-named directories.

Tests. Four new tests. Each one fails with its source change stashed and passes with it.

Gates on f970aed. cargo fmt -- --check exit 0, cargo check exit 0, grid_cache_fallback 12 of 12, grid_error_test 2 of 2, cache_unit_tests 14 of 14, lib 46 of 46.

One Qodo item was a false positive. It said cargo fmt --check fails on a split fs::write call in the test file. The gate exits 0 on the committed tree. A copy with the call collapsed to one line fails the check, and rustfmt asks for the split form that is already committed. The thread has the probe output.

CI. The new head needs a maintainer approval for the fork workflows again, the same as each earlier head. The committer check passed, so the job reports as action_required for that reason alone.

The fallback gate and the five review items are unchanged on this head. The PR body lists the file-name rule, the Grid-subtree scope, the legacy rule and all twelve fallback tests.

Comment thread rust/src/grid.rs Outdated
Comment on lines +328 to +329
let cache_path = self.get_cache_path()?.unwrap_or_default();
let jars = collect_files_from_cache(&cache_path, |entry| self.is_driver(entry));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

3. Grid can use another cache's server jar 🐞 Bug ≡ Correctness

find_best_driver_from_cache collects candidates from the entire cache root, while its ranked
checks validate only the jar’s immediate parent and filename. With
other/4.54.0/selenium-server-4.54.0.jar in that root, an unversioned Grid request can report that
unrelated path as its driver when setup falls back.
Agent Prompt
## Issue description
The new Grid cache lookup can select a matching jar from a non-Grid subtree of the configured cache.

## Fix Focus Areas
- rust/src/grid.rs[327-367]
- rust/tests/grid_tests.rs[118-196]

## Recommended Fix
Scope both ranked and legacy scans to the `grid` cache subtree, preserving the supported legacy layout within that subtree. Add a test with a matching jar under another cache directory and no Grid jar.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in b42da79.

You are right. The ranked scan started at the cache root, so a jar under a sibling directory could match the name and version checks.

Both scans now resolve the Grid cache directory as the cache root joined with the manager's browser name. The ranked scan and the legacy lookup both use that path. The legacy layout within the Grid subtree still works, so a jar directly under grid is still considered.

grid_cache_fallback_ignores_jar_in_other_cache_dir_test puts other/4.54.0/selenium-server-4.54.0.jar in the cache root with no Grid jar, and expects no result.

Probe: with the scope change stashed, cargo test --test grid_tests grid_cache_fallback reports 10 passed and 1 failed. With the change, it reports 11 passed.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 36a6c04

Resolve the Grid cache directory from the cache root and browser name.
Use that subdirectory for both the ranked scan and the legacy fallback.
Add a regression test to ensure jars in sibling cache directories are ignored.
Comment thread rust/src/grid.rs
// while never returning a jar from a SNAPSHOT directory.
find_latest_from_cache(&grid_cache_path, |entry| {
self.is_driver(entry)
&& !entry.path().parent().is_some_and(is_snapshot_dir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

3. Offline grid can return a mismatched jar 🐞 Bug ≡ Correctness

find_best_driver_from_cache rejects a jar whose filename version disagrees with its parent
directory during ranking, but the legacy lookup does not enforce that check. If the cache contains
only grid/4.54.0/selenium-server-4.9.0.jar, the legacy lookup returns that rejected jar and the
offline CLI reports it as a successful fallback.
Agent Prompt
## Issue description
The legacy fallback accepts jars in version-named directories even when the jar version disagrees with the directory version, undoing the ranked lookup's validation.

## Fix Focus Areas
- rust/src/grid.rs[361-370]
- rust/tests/grid_tests.rs[360-384]

## Recommended Fix
Allow the legacy lookup only for jars outside version-named directories, while preserving the snapshot exclusions. Add a test with a mismatched jar as the only cached candidate and assert that no jar is returned.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in 898e12d.

You are right. The ranked pass enforced the file-name rule, and the legacy pass did not. A cache that held only the mismatched jar could still return it.

The legacy filter now also requires that the jar's parent is not a version-named directory. That is the only layout the legacy pass exists for. A jar inside a version-named directory is handled by the ranked pass alone.

grid_cache_fallback_ignores_mismatched_jar_in_version_dir_test puts only grid/4.54.0/selenium-server-4.9.0.jar in the cache, and expects no result.

Probe: with the change stashed, cargo test --test grid_tests grid_cache_fallback reports 11 passed and 1 failed. With the change, it reports 12 passed.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit b42da79

Reject jars inside version-named directories in the legacy cache
fallback pass so mismatched jars are handled only by the ranked pass.
Add a regression test for a mismatched jar in a version directory.
Comment thread rust/tests/grid_tests.rs
Comment on lines +366 to +370
fs::write(
mismatched_dir.join("selenium-server-4.9.0.jar"),
b"fake jar",
)
.unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remediation recommended

3. Grid test file fails the repository formatter check 📘 Rule violation ⚙ Maintainability

The fs::write call in grid_cache_fallback_requires_jar_name_to_match_dir_test is split across
lines, but its arguments (mismatched_dir.join("selenium-server-4.9.0.jar"), b"fake jar") are 57
characters, under rustfmt's 60-character argument-width threshold, so rustfmt collapses it to one
line. The same-width call in grid_cache_fallback_ignores_jar_in_other_cache_dir_test
(fs::write(other_dir.join("selenium-server-4.54.0.jar"), b"fake jar").unwrap();) is already on one
line, so the two sites disagree and the split one is the one rustfmt would change. This fails the
cargo fmt -- --check gate the PR description says it passed.
Agent Prompt
Issue description
The `fs::write` call in the test `grid_cache_fallback_requires_jar_name_to_match_dir_test` in `rust/tests/grid_tests.rs` is split over several lines, but rustfmt keeps it on one line because its arguments are under the 60-character threshold. This breaks the repository formatter check.

Fix Focus Areas
- rust/tests/grid_tests.rs[366-370]

Recommended Fix
Replace the multi-line call with `fs::write(mismatched_dir.join("selenium-server-4.9.0.jar"), b"fake jar").unwrap();` on one line, matching the style of the `fs::write(other_dir.join(...))` call in `grid_cache_fallback_ignores_jar_in_other_cache_dir_test`. Then run `cargo fmt -- --check` to confirm.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Checked this one with rustfmt itself, and the claim does not hold.

cargo fmt --check and cargo fmt -- --check both exit 0 on the committed tree.

The current split is rustfmt's own output. I copied the test file, collapsed the call to one line as suggested, and ran rustfmt --edition 2024 --check on both copies. The copy as committed exits 0. The collapsed copy exits 1, and rustfmt asks for exactly the split form that is in the file:

-    fs::write(mismatched_dir.join("selenium-server-4.9.0.jar"), b"fake jar").unwrap();
+    fs::write(
+        mismatched_dir.join("selenium-server-4.9.0.jar"),
+        b"fake jar",
+    )
+    .unwrap();

The call is the receiver of a method chain, so the chain width applies, not the 60-character argument width. Applying the suggested change would break the formatter gate.

Comment thread rust/src/grid.rs Outdated
// while never returning a jar from a SNAPSHOT directory.
find_latest_from_cache(&grid_cache_path, |entry| {
self.is_driver(entry)
// The legacy pass covers only jars outside a version-named directory.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Informational

8. A legacy-pass comment restates the code it sits on 📘 Rule violation ⚙ Maintainability

The comment The legacy pass covers only jars outside a version-named directory. sits above the
.and_then(version_from_dir_name).is_none() predicate and only paraphrases that check. It gives no
rationale for why the legacy pass must exclude version-named directories, which is the detail a
later reader would need before changing the filter.
Agent Prompt
Issue description
The comment above the legacy-pass predicate in `GridManager::find_best_driver_from_cache` in `rust/src/grid.rs` only restates the `version_from_dir_name(...).is_none()` check. It does not explain why version-named directories are excluded from the legacy pass, so a later reader could remove the exclusion without understanding the risk.

Fix Focus Areas
- rust/src/grid.rs[361-370]

Recommended Fix
Replace the comment with one that states the reason, for example: the ranked pass already validates jars in version-named directories against their file-name version, so the legacy pass must skip them to avoid returning an unvalidated jar from the same directory.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fair point. The comment now gives the reason.

f970aed replaces it with:

// The ranked pass above already checks a jar in a version-named directory
// against its file-name version. The legacy pass must skip those
// directories, or it could return a jar the ranked pass rejected.

Comment text only. cargo fmt --check exits 0, and the 12 fallback tests still pass.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 898e12d

Explain that the ranked pass already checks jars in version-named directories, so the legacy pass must skip them to avoid returning a jar that the ranked pass rejected.
@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit f970aed

@bonigarcia

Copy link
Copy Markdown
Member

Thanks, is_grid() and the latest changes look good. CI failed on 0ac3bc6, though (run), and the newer commits don't address it: integration_grid_tests_test fails on Linux ARM, macOS and Windows.

CI sets SE_CACHE_PATH, which takes precedence over --cache-path, so the CLI tests use the shared cache instead of the temp one (e.g. they find the real grid/4.10.0 jar). I should have mentioned that when I asked for these tests. Running the tests locally with SE_CACHE_PATH set to another directory should reproduce it.

Please add cmd.env_remove("SE_CACHE_PATH"); to the four tests that pass --cache-path, as cache_tests.rs and config_tests.rs do. That includes grid_error_test, which passes today but doesn't use its fake jar in CI.

@fuleinist

Copy link
Copy Markdown
Author

Added cmd.env_remove("SE_CACHE_PATH"); to the four tests in rust/tests/grid_tests.rs that pass --cache-path. That includes grid_error_test. The new line sits right after let mut cmd = get_selenium_manager();, matching cache_tests.rs and config_tests.rs.

I ran the full grid_tests file twice. Both runs passed 20/20. One run had SE_CACHE_PATH set to a scratch directory, which reproduces the CI condition. The other run had it unset.

Pushed as 2c52a07.

@qodo-code-review

Copy link
Copy Markdown
Contributor

Code review by qodo was updated up to the latest commit 2c52a07

This branch has not been deployed

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

Labels

B-manager Selenium Manager C-rust Rust code is mostly Selenium Manager

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: Selenium Manager can't find a cached Grid jar in its offline fallback on Linux and Windows

5 participants