Skip to content
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

cargo: Fix sparse registry error on update #7066

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "dependabot/cargo/file_updater"
require "dependabot/cargo/file_updater/manifest_updater"
require "dependabot/cargo/file_parser"
require "dependabot/cargo/toolchain_parser"
require "dependabot/shared_helpers"
module Dependabot
module Cargo
Expand All @@ -32,7 +33,8 @@ def updated_lockfile_content
SharedHelpers.with_git_configured(credentials: credentials) do
# Shell out to Cargo, which handles everything for us, and does
# so without doing an install (so it's fast).
run_shell_command("cargo update -p #{dependency_spec}", fingerprint: "cargo update -p <dependency_spec>")
run_shell_command("cargo #{toolchain_parser.sparse_flag} update -p #{dependency_spec}",
fingerprint: "cargo update -p <dependency_spec>")
end

updated_lockfile = File.read("Cargo.lock")
Expand Down Expand Up @@ -369,6 +371,10 @@ def toolchain
dependency_files.find { |f| f.name == "rust-toolchain" }
end

def toolchain_parser
@toolchain_parser ||= Cargo::ToolchainParser.new(toolchain)
end

def virtual_manifest?(file)
!file.content.include?("[package]")
end
Expand Down
37 changes: 37 additions & 0 deletions cargo/lib/dependabot/cargo/toolchain_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "toml-rb"

module Dependabot
module Cargo
class ToolchainParser
def initialize(toolchain)
@toolchain = toolchain
end

def sparse_flag
return @sparse_flag if defined?(@sparse_flag)

@sparse_flag = needs_sparse_flag ? "-Z sparse-registry" : ""
end

private

attr_reader :toolchain

# We only need to set the -Z sparse-registry flag for nightly and unstable toolchains
# during which the feature exists and is reading the environment variable CARGO_REGISTRIES_CRATES_IO_PROTOCOL.
def needs_sparse_flag
return false unless toolchain

channel = TomlRB.parse(toolchain.content).fetch("toolchain", nil)&.fetch("channel", nil)
return false unless channel

date = channel.match(/nightly-(\d{4}-\d{2}-\d{2})/)&.captures&.first
return false unless date

Date.parse(date).between?(Date.parse("2022-07-10"), Date.parse("2023-01-20"))
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
require "dependabot/cargo/update_checker"
require "dependabot/cargo/file_parser"
require "dependabot/cargo/version"
require "dependabot/cargo/toolchain_parser"
require "dependabot/errors"

module Dependabot
module Cargo
class UpdateChecker
Expand Down Expand Up @@ -134,7 +136,7 @@ def dependency_spec
# so without doing an install (so it's fast).
def run_cargo_update_command
run_cargo_command(
"cargo update -p #{dependency_spec} --verbose",
"cargo #{toolchain_parser.sparse_flag} update -p #{dependency_spec} --verbose",
fingerprint: "cargo update -p <dependency_spec> --verbose"
)
end
Expand Down Expand Up @@ -407,6 +409,10 @@ def toolchain
find { |f| f.name == "rust-toolchain" }
end

def toolchain_parser
@toolchain_parser ||= Cargo::ToolchainParser.new(toolchain)
end

def git_dependency?
GitCommitChecker.new(
dependency: dependency,
Expand Down
42 changes: 42 additions & 0 deletions cargo/spec/dependabot/cargo/toolchain_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require "dependabot/dependency_file"
require "dependabot/cargo/toolchain_parser"

RSpec.describe Dependabot::Cargo::ToolchainParser do
it "returns sparse-registry for nightlies in a certain range" do
toolchain = Dependabot::DependencyFile.new(
name: "rust-toolchain",
content: "[toolchain]\nchannel = \"nightly-2022-07-10\""
)
expect(described_class.new(toolchain).sparse_flag).to eq("-Z sparse-registry")
end

it "doesn't return sparse-registry for stable" do
toolchain = Dependabot::DependencyFile.new(
name: "rust-toolchain",
content: "[toolchain]\nchannel = \"stable\""
)
expect(described_class.new(toolchain).sparse_flag).to eq("")
end

it "doesn't return sparse-registry when no toolchain file" do
expect(described_class.new(nil).sparse_flag).to eq("")
end

it "doesn't return sparse-registry for nightlies outside the range" do
toolchain = Dependabot::DependencyFile.new(
name: "rust-toolchain",
content: "[toolchain]\nchannel = \"nightly-2023-01-21\""
)
expect(described_class.new(toolchain).sparse_flag).to eq("")
end

it "doesn't return sparse-registry when the channel isn't specified" do
toolchain = Dependabot::DependencyFile.new(
name: "rust-toolchain",
content: "[toolchain]"
)
expect(described_class.new(toolchain).sparse_flag).to eq("")
end
end