Skip to content

Commit

Permalink
Fixes #8555 (#8566)
Browse files Browse the repository at this point in the history
* Fixes #8555

* Make this closer to what npm does

---------

Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner authored Jan 30, 2024
1 parent 4989ef8 commit 2eede4f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
36 changes: 33 additions & 3 deletions src/install/install.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,40 @@ pub const PackageInstall = struct {
var package_json_checker = json_parser.PackageJSONVersionChecker.init(allocator, &source, &log) catch return false;
_ = package_json_checker.parseExpr() catch return false;
if (!package_json_checker.has_found_name or !package_json_checker.has_found_version or log.errors > 0) return false;
const found_version = package_json_checker.found_version;
// Check if the version matches
if (!strings.eql(found_version, this.package_version)) {
const offset = brk: {
// ASCII only.
for (0..found_version.len) |c| {
switch (found_version[c]) {
// newlines & whitespace
' ',
'\t',
'\n',
'\r',
std.ascii.control_code.vt,
std.ascii.control_code.ff,

// version separators
'v',
'=',
=> {},
else => {
break :brk c;
},
}
}
// If we didn't find any of these characters, there's no point in checking the version again.
// it will never match.
return false;
};

if (!strings.eql(found_version[offset..], this.package_version)) return false;
}

// Version is more likely to not match than name, so we check it first.
return strings.eql(package_json_checker.found_version, this.package_version) and
strings.eql(package_json_checker.found_name, this.package_name);
// lastly, check the name.
return strings.eql(package_json_checker.found_name, this.package_name);
}

pub const Result = union(Tag) {
Expand Down
28 changes: 19 additions & 9 deletions src/install/semver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1043,17 +1043,27 @@ pub const Version = extern struct {

var i: usize = 0;

i += strings.lengthOfLeadingWhitespaceASCII(input[i..]);
if (i == input.len) {
result.valid = false;
return result;
}

if (input[i] == 'v' or input[i] == '=') {
i += 1;
for (0..input.len) |c| {
switch (input[c]) {
// newlines & whitespace
' ',
'\t',
'\n',
'\r',
std.ascii.control_code.vt,
std.ascii.control_code.ff,

// version separators
'v',
'=',
=> {},
else => {
i = c;
break;
},
}
}

i += strings.lengthOfLeadingWhitespaceASCII(input[i..]);
if (i == input.len) {
result.valid = false;
return result;
Expand Down

0 comments on commit 2eede4f

Please sign in to comment.