Skip to content

Commit

Permalink
cli: store cli in user data dir, separate per quality (microsoft#182102)
Browse files Browse the repository at this point in the history
* cli: store cli in user data dir, separate per quality

Fixes microsoft#181017

On first run, the `~/.vscode-cli` will be migrated inside the user data dir of the currently running quality.

* use create_dir_all instead

* clippy fixes
  • Loading branch information
connor4312 authored May 10, 2023
1 parent 8b11c81 commit d3d9f86
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
3 changes: 2 additions & 1 deletion build/azure-pipelines/cli/prepare.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions build/azure-pipelines/cli/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const setLauncherEnvironmentVars = () => {
['VSCODE_CLI_TUNNEL_SERVICE_MUTEX', product.win32TunnelServiceMutex],
['VSCODE_CLI_TUNNEL_CLI_MUTEX', product.win32TunnelMutex],
['VSCODE_CLI_COMMIT', commit],
['VSCODE_CLI_DEFAULT_PARENT_DATA_DIR', product.dataFolderName],
[
'VSCODE_CLI_WIN32_APP_IDS',
!isOSS && JSON.stringify(
Expand Down
2 changes: 1 addition & 1 deletion cli/src/bin/code/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() -> Result<(), std::convert::Infallible> {
});

let core = parsed.core();
let context_paths = LauncherPaths::new(&core.global_options.cli_data_dir).unwrap();
let context_paths = LauncherPaths::migrate(core.global_options.cli_data_dir.clone()).unwrap();
let context_args = core.clone();

// gets a command context without installing the global logger
Expand Down
6 changes: 6 additions & 0 deletions cli/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ pub const TUNNEL_ACTIVITY_NAME: &str = concatcp!(PRODUCT_NAME_LONG, " Tunnel");

const NONINTERACTIVE_VAR: &str = "VSCODE_CLI_NONINTERACTIVE";

/// Default data CLI data directory.
pub const DEFAULT_DATA_PARENT_DIR: &str = match option_env!("VSCODE_CLI_DEFAULT_PARENT_DATA_DIR") {
Some(n) => n,
None => ".vscode-oss",
};

pub fn get_default_user_agent() -> String {
format!(
"vscode-server-launcher/{}",
Expand Down
52 changes: 42 additions & 10 deletions cli/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
extern crate dirs;

use std::{
fs::{create_dir, read_to_string, remove_dir_all, write},
fs::{create_dir_all, read_to_string, remove_dir_all, write},
path::{Path, PathBuf},
sync::{Arc, Mutex},
};

use serde::{de::DeserializeOwned, Serialize};

use crate::{
constants::VSCODE_CLI_QUALITY,
constants::{DEFAULT_DATA_PARENT_DIR, VSCODE_CLI_QUALITY},
download_cache::DownloadCache,
util::errors::{wrap, AnyError, NoHomeForLauncherError, WrappedError},
};
Expand Down Expand Up @@ -107,8 +107,38 @@ where
}

impl LauncherPaths {
pub fn new(root: &Option<String>) -> Result<LauncherPaths, AnyError> {
let root = root.as_deref().unwrap_or("~/.vscode-cli");
/// todo@conno4312: temporary migration from the old CLI data directory
pub fn migrate(root: Option<String>) -> Result<LauncherPaths, AnyError> {
if root.is_some() {
return Self::new(root);
}

let home_dir = match dirs::home_dir() {
None => return Self::new(root),
Some(d) => d,
};

let old_dir = home_dir.join(".vscode-cli");
let mut new_dir = home_dir;
new_dir.push(DEFAULT_DATA_PARENT_DIR);
new_dir.push("cli");
if !old_dir.exists() || new_dir.exists() {
return Self::new_for_path(new_dir);
}

if let Err(e) = std::fs::rename(&old_dir, &new_dir) {
// no logger exists at this point in the lifecycle, so just log to stderr
eprintln!(
"Failed to migrate old CLI data directory, will create a new one ({})",
e
);
}

Self::new_for_path(new_dir)
}

pub fn new(root: Option<String>) -> Result<LauncherPaths, AnyError> {
let root = root.unwrap_or_else(|| format!("~/{}/cli", DEFAULT_DATA_PARENT_DIR));
let mut replaced = root.to_owned();
for token in HOME_DIR_ALTS {
if root.contains(token) {
Expand All @@ -120,14 +150,16 @@ impl LauncherPaths {
}
}

if !Path::new(&replaced).exists() {
create_dir(&replaced)
.map_err(|e| wrap(e, format!("error creating directory {}", &replaced)))?;
Self::new_for_path(PathBuf::from(replaced))
}

fn new_for_path(root: PathBuf) -> Result<LauncherPaths, AnyError> {
if !root.exists() {
create_dir_all(&root)
.map_err(|e| wrap(e, format!("error creating directory {}", root.display())))?;
}

Ok(LauncherPaths::new_without_replacements(PathBuf::from(
replaced,
)))
Ok(LauncherPaths::new_without_replacements(root))
}

pub fn new_without_replacements(root: PathBuf) -> LauncherPaths {
Expand Down

0 comments on commit d3d9f86

Please sign in to comment.