Repository: denoland/deno
Pull request: fix(flags): implement a better sandbox subcommand


Changed files:
filename: cli/args/flags.rs
patch: @@ -224,6 +224,11 @@ pub struct CoverageFlags {
   pub r#type: CoverageType,
 }
 
+#[derive(Clone, Debug, Eq, PartialEq, Default)]
+pub struct DeployFlags {
+  pub sandbox: bool,
+}
+
 #[derive(Clone, Debug, Eq, PartialEq, Default)]
 pub enum DocSourceFileFlag {
   #[default]
@@ -598,7 +603,7 @@ pub enum DenoSubcommand {
   Compile(CompileFlags),
   Completions(CompletionsFlags),
   Coverage(CoverageFlags),
-  Deploy(Option<&'static str>),
+  Deploy(DeployFlags),
   Doc(DocFlags),
   Eval(EvalFlags),
   Fmt(FmtFlags),
@@ -1671,14 +1676,14 @@ pub fn flags_from_vec_with_initial_cwd(
     deploy_parse(
       &mut flags,
       &mut matches.remove_subcommand().unwrap().1,
-      None,
+      false,
     )?;
     return Ok(flags);
   } else if matches.subcommand_matches("sandbox").is_some() {
     deploy_parse(
       &mut flags,
       &mut matches.remove_subcommand().unwrap().1,
-      Some("sandbox"),
+      true,
     )?;
     return Ok(flags);
   }
@@ -1734,12 +1739,12 @@ pub fn flags_from_vec_with_initial_cwd(
     if subcommand.get_name() == "deploy" {
       flags.argv = vec![String::from("--help")];
       flags.permissions.allow_all = true;
-      flags.subcommand = DenoSubcommand::Deploy(None);
+      flags.subcommand = DenoSubcommand::Deploy(DeployFlags::default());
       return Ok(flags);
     } else if subcommand.get_name() == "sandbox" {
       flags.argv = vec![String::from("--help")];
       flags.permissions.allow_all = true;
-      flags.subcommand = DenoSubcommand::Deploy(Some("sandbox"));
+      flags.subcommand = DenoSubcommand::Deploy(DeployFlags { sandbox: true });
       return Ok(flags);
     }
 
@@ -5911,7 +5916,7 @@ fn coverage_parse(
 fn deploy_parse(
   flags: &mut Flags,
   matches: &mut ArgMatches,
-  subcommand: Option<&'static str>,
+  sandbox: bool,
 ) -> clap::error::Result<()> {
   let mut args: Vec<String> = matches
     .remove_many("args")
@@ -5923,7 +5928,7 @@ fn deploy_parse(
   }
 
   flags.argv = args;
-  flags.subcommand = DenoSubcommand::Deploy(subcommand);
+  flags.subcommand = DenoSubcommand::Deploy(DeployFlags { sandbox });
   Ok(())
 }
 

filename: cli/main.rs
patch: @@ -147,8 +147,8 @@ async fn run_subcommand(
       );
       tools::bundle::bundle(flags, bundle_flags).await
     }),
-    DenoSubcommand::Deploy(subcommand) => spawn_subcommand(async move {
-      tools::deploy::deploy(Arc::unwrap_or_clone(flags), subcommand).await
+    DenoSubcommand::Deploy(deploy_flags) => spawn_subcommand(async move {
+      tools::deploy::deploy(Arc::unwrap_or_clone(flags), deploy_flags).await
     }),
     DenoSubcommand::Doc(doc_flags) => {
       spawn_subcommand(async { tools::doc::doc(flags, doc_flags).await })

filename: cli/tools/deploy.rs
patch: @@ -9,6 +9,7 @@ use deno_path_util::ResolveUrlOrPathError;
 use deno_runtime::WorkerExecutionMode;
 use deno_runtime::deno_permissions::PermissionsContainer;
 
+use crate::args::DeployFlags;
 use crate::args::Flags;
 use crate::args::jsr_api_url;
 use crate::factory::CliFactory;
@@ -17,12 +18,15 @@ use crate::registry;
 
 pub async fn deploy(
   mut flags: Flags,
-  subcommand: Option<&'static str>,
+  deploy_flags: DeployFlags,
 ) -> Result<i32, AnyError> {
   flags.node_modules_dir = Some(NodeModulesDirMode::None);
   flags.no_lock = true;
-  if let Some(subcommand) = subcommand {
-    flags.argv = vec![subcommand.to_string()];
+  if deploy_flags.sandbox {
+    // SAFETY: only this subcommand is running, nothing else, so it's safe to set an env var.
+    unsafe {
+      std::env::set_var("DENO_DEPLOY_CLI_SANDBOX", "1");
+    }
   }
 
   let mut factory = CliFactory::from_flags(Arc::new(flags));