From 1fd3c4b714894c76976593273372bdacf5a61248 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 14 Jul 2026 15:47:00 -0700 Subject: [PATCH] Add `--ignore-settings-file` and `DSC_IGNORE_SETTINGS_FILE` env var --- dsc/locales/en-us.toml | 1 + dsc/src/args.rs | 2 ++ dsc/src/main.rs | 11 +++++++++-- dsc/tests/dsc_settings.tests.ps1 | 19 +++++++++++++++++++ lib/dsc-lib/locales/en-us.toml | 1 + lib/dsc-lib/src/util.rs | 7 +++++++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/dsc/locales/en-us.toml b/dsc/locales/en-us.toml index 49e9b50dd..3e867878c 100644 --- a/dsc/locales/en-us.toml +++ b/dsc/locales/en-us.toml @@ -37,6 +37,7 @@ listFunctionAbout = "List or find functions" version = "The version of the resource to invoke in semver format" serverAbout = "Use DSC as a server over JSON-RPC (useful as MCP server)" bicepAbout = "Use DSC as a Bicep server over gRPC" +ignoreSettingsFile = "Ignore the settings file when running the command" [main] failedToSpawnMain = "Failed to spawn dsc main thread: %{error}" diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 4d4622287..284c8d023 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -55,6 +55,8 @@ pub struct Args { pub trace_format: Option, #[clap(short = 'p', long, help = t!("args.progressFormat").to_string(), value_enum)] pub progress_format: Option, + #[clap(short = 'i', long, help = t!("args.ignoreSettingsFile").to_string())] + pub ignore_settings_file: bool, } #[derive(Debug, PartialEq, Eq, Subcommand)] diff --git a/dsc/src/main.rs b/dsc/src/main.rs index 9414d677d..71d25fe65 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -4,10 +4,10 @@ use args::{Args, SubCommand}; use clap::{CommandFactory, Parser}; use clap_complete::generate; -use dsc_lib::progress::ProgressFormat; +use dsc_lib::{progress::ProgressFormat, util::DSC_IGNORE_SETTINGS_FILE}; use server::start_server; use rust_i18n::{i18n, t}; -use std::{io, process::exit}; +use std::{env::set_var, io, process::exit}; use sysinfo::{Process, RefreshKind, System, get_current_pid, ProcessRefreshKind}; use tracing::{error, info, warn, debug}; @@ -67,6 +67,13 @@ fn dsc_main() { let args = Args::parse(); + // if args.ignore_settings_file is set, we create an env var called `DSC_IGNORE_SETTINGS_FILE` set to value 1 + if args.ignore_settings_file { + unsafe { + set_var(DSC_IGNORE_SETTINGS_FILE, "1"); + } + } + util::enable_tracing(args.trace_level.as_ref(), args.trace_format.as_ref()); debug!("{}: {}", t!("main.usingDscVersion"), env!("CARGO_PKG_VERSION")); diff --git a/dsc/tests/dsc_settings.tests.ps1 b/dsc/tests/dsc_settings.tests.ps1 index 4717f63fd..555f5955f 100644 --- a/dsc/tests/dsc_settings.tests.ps1 +++ b/dsc/tests/dsc_settings.tests.ps1 @@ -120,4 +120,23 @@ Describe 'tests for dsc settings' { "$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Trace-level is Trace" "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Using Resource Path: Defaultv1SettingsDir' } + + It 'DSC_IGNORE_SETTINGS_FILE environment variable disables settings file' { + $oldEnv = $env:DSC_IGNORE_SETTINGS_FILE + try { + $env:DSC_IGNORE_SETTINGS_FILE = "1" + $null = dsc -l warn resource list 2> $TestDrive/tracing.txt + $errorLog = Get-Content "$TestDrive/tracing.txt" -Raw + $errorLog | Should -BeLike "*WARN*Ignoring settings file due to environment variable 'DSC_IGNORE_SETTINGS_FILE' being set or '--ignore-settings-file' flag being used*" + } + finally { + $env:DSC_IGNORE_SETTINGS_FILE = $oldEnv + } + } + + It '--ignore-settings-file command-line argument disables settings file' { + $null = dsc --ignore-settings-file resource list 2> $TestDrive/tracing.txt + $errorLog = Get-Content "$TestDrive/tracing.txt" -Raw + $errorLog | Should -BeLike "*WARN*Ignoring settings file due to environment variable 'DSC_IGNORE_SETTINGS_FILE' being set or '--ignore-settings-file' flag being used*" + } } diff --git a/lib/dsc-lib/locales/en-us.toml b/lib/dsc-lib/locales/en-us.toml index f5ad7c555..c43567fb4 100644 --- a/lib/dsc-lib/locales/en-us.toml +++ b/lib/dsc-lib/locales/en-us.toml @@ -934,6 +934,7 @@ executableNotFound = "Executable '%{executable}' not found" policyFolderNotSecure = "Policy folder '%{path}' is not secure, settings file will not be used. Required permissions: %{required}" policyFolderNotSecureLinux = "Only root should have write access (no group/other write bits)" policyFolderNotSecureWindows = "Only SYSTEM and Administrators should have write access" +ignoreSettingsFile = "Ignoring settings file due to environment variable 'DSC_IGNORE_SETTINGS_FILE' being set or '--ignore-settings-file' flag being used" [types.date_version] invalidDay = "day `%{day}` for month `%{month}` is invalid - must be between `01` and `%{max_days}`" diff --git a/lib/dsc-lib/src/util.rs b/lib/dsc-lib/src/util.rs index 9ced77575..37ec48717 100644 --- a/lib/dsc-lib/src/util.rs +++ b/lib/dsc-lib/src/util.rs @@ -14,6 +14,8 @@ use std::{ use tracing::{debug, warn}; use which::which; +pub const DSC_IGNORE_SETTINGS_FILE: &str = "DSC_IGNORE_SETTINGS_FILE"; + pub struct DscSettingValue { pub setting: Value, pub policy: Value, @@ -172,6 +174,11 @@ pub fn get_setting(value_name: &str) -> Result { let mut result: DscSettingValue = DscSettingValue::default(); let mut settings_file_path : PathBuf; + if env::var(DSC_IGNORE_SETTINGS_FILE).is_ok() { + warn!("{}", t!("util.ignoreSettingsFile")); + return Ok(result); + } + if let Some(exe_home) = get_exe_path()?.parent() { // First, get setting from the default settings file settings_file_path = exe_home.join(DEFAULT_SETTINGS_FILE_NAME);