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

fix: Fix proto use installing global versions. #326

Merged
merged 6 commits into from
Dec 7, 2023
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#### 🐞 Fixes

- Fixed an issue where `proto use` would install tools from `~/.proto/.prototools`.
- Fixed an issue where our directory locking would fail on Windows when the inventory path was overwritten.
- Fixed stable being considered a latest alias.

#### ⚙️ Internal
Expand Down
5 changes: 4 additions & 1 deletion crates/cli/src/commands/install_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub async fn install_all(proto: ResourceRef<ProtoResource>) {

debug!("Detecting tool versions to install");

let config = proto.env.load_config()?;
let config = proto
.env
.load_config_manager()?
.get_merged_config_without_global()?;
let mut versions = config.versions.to_owned();

for tool in &tools {
Expand Down
9 changes: 6 additions & 3 deletions crates/cli/tests/plugins_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,14 @@ mod plugins {
fn supports_rust() {
let sandbox = create_empty_sandbox();

create_proto_command(sandbox.path())
let assert = create_proto_command(sandbox.path())
.arg("install")
.arg("rust")
.assert()
.success();
.assert();

starbase_sandbox::debug_process_output(assert.get_output());

assert.success();
}

#[test]
Expand Down
19 changes: 19 additions & 0 deletions crates/cli/tests/use_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,23 @@ deno = "1.30.0"

assert!(node_path.exists());
}

#[test]
fn doesnt_install_global_tools() {
let sandbox = create_empty_sandbox();
let node_path = sandbox.path().join(".proto/tools/node/19.0.0");
let deno_path = sandbox.path().join(".proto/tools/deno/1.30.0");

sandbox.create_file(".prototools", r#"node = "19.0.0""#);
sandbox.create_file(".proto/.prototools", r#"deno = "1.30.0""#);

assert!(!node_path.exists());
assert!(!deno_path.exists());

let mut cmd = create_proto_command(sandbox.path());
cmd.arg("use").assert().success();

assert!(node_path.exists());
assert!(!deno_path.exists());
}
}
1 change: 1 addition & 0 deletions crates/core/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl ProtoEnvironment {

manager.files.push(ProtoConfigFile {
exists: path.exists(),
global: true,
path,
config: ProtoConfig::load_from(&self.root, true)?,
});
Expand Down
45 changes: 31 additions & 14 deletions crates/core/src/proto_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ impl ProtoConfig {
#[derive(Debug)]
pub struct ProtoConfigFile {
pub exists: bool,
pub global: bool,
pub path: PathBuf,
pub config: PartialProtoConfig,
}
Expand Down Expand Up @@ -336,6 +337,7 @@ impl ProtoConfigManager {

files.push(ProtoConfigFile {
exists: path.exists(),
global: false,
path,
config: ProtoConfig::load_from(dir, false)?,
});
Expand All @@ -354,26 +356,41 @@ impl ProtoConfigManager {
}

pub fn get_merged_config(&self) -> miette::Result<&ProtoConfig> {
self.merged_config.get_or_try_init(|| {
self.merged_config
.get_or_try_init(|| self.merge_configs(true))
}

pub fn get_merged_config_without_global(&self) -> miette::Result<ProtoConfig> {
self.merge_configs(false)
}

fn merge_configs(&self, with_global: bool) -> miette::Result<ProtoConfig> {
if with_global {
debug!("Merging loaded configs");
} else {
debug!("Merging loaded configs without global");
}

let mut partial = PartialProtoConfig::default();
let mut count = 0;
let context = &();
let mut partial = PartialProtoConfig::default();
let mut count = 0;
let context = &();

for file in self.files.iter().rev() {
if file.exists {
partial.merge(context, file.config.to_owned())?;
count += 1;
}
for file in self.files.iter().rev() {
if !with_global && file.global {
continue;
}

let mut config = ProtoConfig::from_partial(partial.finalize(context)?);
config.inherit_builtin_plugins();
if file.exists {
partial.merge(context, file.config.to_owned())?;
count += 1;
}
}

let mut config = ProtoConfig::from_partial(partial.finalize(context)?);
config.inherit_builtin_plugins();

debug!("Merged {} configs", count);
debug!("Merged {} configs", count);

Ok(config)
})
Ok(config)
}
}
12 changes: 11 additions & 1 deletion crates/core/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,19 @@ impl Tool {
}

let install_dir = self.get_tool_dir();
let install_lock = fs::lock_directory(&install_dir)?;
let mut installed = false;

// Lock the install directory. If the inventory has been overridden,
// lock the internal proto tool directory instead.
let install_lock = fs::lock_directory(if self.metadata.inventory.override_dir.is_some() {
self.proto
.tools_dir
.join(self.id.as_str())
.join(self.get_resolved_version().to_string())
} else {
install_dir.clone()
})?;

self.on_installing
.emit(InstallingEvent {
version: self.get_resolved_version(),
Expand Down