-
Notifications
You must be signed in to change notification settings - Fork 80
Fix Cargo version parsing for workspace inheritance syntax #1614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix Cargo version parsing for workspace inheritance syntax #1614
Conversation
|
|
||
| String name = packageTable.getString(NAME_KEY); | ||
| String version = null; | ||
| Object versionObj = packageTable.get(VERSION_KEY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we want to do for conditions when versionObj is null, I have seen examples in python where version is not specified, do we have similar concept in Cargo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an interesting question which I didn't think of earlier. I'll explore further whether there is any possibility of an version section being null and if so, then how cargo declares / handles this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have investigated further how cargo treats version field, and current behavior of the cargo detectors when version field is missing i.e. versionObj is null. I'm sharing my findings here,
Yes, versionObj can be null. The the version field has been optional in Cargo.toml since Cargo 1.75 (defaulting to 0.0.0 if omitted). [Reference]
However, the behavior differs based on project structure:
1. Projects Using Workspace Inheritance Syntax
Cargo CLI Detector:
- Depends on
cargo treecommand (introduced in Cargo 1.44) - When
version.workspace = truebut workspace doesn't define version,cargo treecommand fails - The detector never reaches the
CargoTomlParserlogic - Falls back to Cargo Lockfile Detector
Cargo Lockfile Detector:
- Successfully parses
Cargo.tomleven with null/missing version - Defaults to "Default Detect Version" when version is
null
2. Standalone Projects (No Workspace)
Both Cargo CLI Detector and Cargo Lockfile Detector:
- Successfully parse
Cargo.tomlwith null/missing version - Both default to "Default Detect Version" when version is
null cargo treecommand succeeds even without an explicit version
So, the version field is required, when:
- Publishing to crates.io: Always required
- Workspace inheritance: Workspace must define the version (enforced by
cargo tree) - Local development: Optional since Cargo 1.75 (defaults to
0.0.0)
Conclusion
Our implementation of the current merge request safely handles null by returning Optional.of(new NameVersion(name, null)). When version is null, both detectors already default to "Default Detect Version"
JIRA Ticket
IDETECT-4925
Description
Both Cargo detectors were failing to parse name and version from Cargo projects using workspace inheritance syntax
(version.workspace = true). For example, a validCargo.toml,The
CargoTomlParserwas incorrectly assuming version to be a string, but in cases of workspace inheritance, version is represented as a toml table. This causedTomlInvalidTypeExceptionduring dependency extraction.Proposed Fix:
CargoTomlParserto detect when version is a table and handle workspace inheritance correctly.Cargo.tomlfiles withversion.workspace = true.Note: This is meant to be shipped with detect 11.2 release.