From 2eede4f43510a1b49c34af56c5b22281e257a021 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 29 Jan 2024 17:52:08 -0800 Subject: [PATCH] Fixes #8555 (#8566) * Fixes #8555 * Make this closer to what npm does --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/install/install.zig | 36 +++++++++++++++++++++++++++++++++--- src/install/semver.zig | 28 +++++++++++++++++++--------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/install/install.zig b/src/install/install.zig index 2a07a5a6d8f0c9..0aba136aeef49d 100644 --- a/src/install/install.zig +++ b/src/install/install.zig @@ -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) { diff --git a/src/install/semver.zig b/src/install/semver.zig index b1fafd5f470b79..2942c384ca3065 100644 --- a/src/install/semver.zig +++ b/src/install/semver.zig @@ -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;