Skip to content
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
11 changes: 11 additions & 0 deletions src/shell/commands/shopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ impl ShellCommand for ShoptCommand {
"off"
}
));
let _ = context.stdout.write_line(&format!(
"globstar\t{}",
if current_options.contains(ShellOptions::GLOBSTAR) {
"on"
} else {
"off"
}
));
let _ = context.stdout.write_line(&format!(
"nullglob\t{}",
if current_options.contains(ShellOptions::NULLGLOB) {
Expand Down Expand Up @@ -122,6 +130,7 @@ fn parse_option_name(name: &str) -> Option<ShellOptions> {
match name {
"nullglob" => Some(ShellOptions::NULLGLOB),
"failglob" => Some(ShellOptions::FAILGLOB),
"globstar" => Some(ShellOptions::GLOBSTAR),
_ => None,
}
}
Expand All @@ -131,6 +140,8 @@ fn option_to_name(opt: ShellOptions) -> &'static str {
"nullglob"
} else if opt == ShellOptions::FAILGLOB {
"failglob"
} else if opt == ShellOptions::GLOBSTAR {
"globstar"
} else {
"unknown"
}
Expand Down
24 changes: 21 additions & 3 deletions src/shell/execute.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ffi::OsString;
Expand Down Expand Up @@ -858,11 +859,29 @@ fn evaluate_word_parts(
}
let is_absolute = Path::new(&current_text).is_absolute();
let cwd = state.cwd();
let options = state.shell_options();

// when globstar is disabled, replace ** with * so it doesn't match
// across directory boundaries (the glob crate always treats ** as recursive)
let pattern_text: Cow<str> = if !options.contains(ShellOptions::GLOBSTAR)
&& current_text.contains("**")
{
// repeatedly replace ** with * to handle cases like *** -> ** -> *
let mut result = current_text.clone();
while result.contains("**") {
result = result.replace("**", "*");
}
Cow::Owned(result)
} else {
Cow::Borrowed(&current_text)
};

let pattern = if is_absolute {
current_text.clone()
pattern_text.into_owned()
} else {
format!("{}/{}", cwd.display(), current_text)
format!("{}/{}", cwd.display(), pattern_text)
};

let result = glob::glob_with(
&pattern,
glob::MatchOptions {
Expand All @@ -879,7 +898,6 @@ fn evaluate_word_parts(
let paths =
paths.into_iter().filter_map(|p| p.ok()).collect::<Vec<_>>();
if paths.is_empty() {
let options = state.shell_options();
// failglob - error when set
if options.contains(ShellOptions::FAILGLOB) {
Err(EvaluateWordTextError::NoFilesMatched { pattern })
Expand Down
6 changes: 4 additions & 2 deletions src/shell/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ bitflags! {
/// When set, pipeline exit code is the rightmost non-zero exit code.
/// Set via `set -o pipefail`.
const PIPEFAIL = 1 << 2;
/// When set, the pattern `**` used in a pathname expansion context will
/// match all files and zero or more directories and subdirectories.
const GLOBSTAR = 1 << 3;
}
}

impl Default for ShellOptions {
fn default() -> Self {
// failglob is on by default to preserve existing deno_task_shell behavior
ShellOptions::FAILGLOB
ShellOptions::FAILGLOB.union(ShellOptions::GLOBSTAR)
}
}

Expand Down
82 changes: 75 additions & 7 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ async fn glob_basic() {
.run()
.await;

// ** matches recursively (globstar on by default)
TestBuilder::new()
.directory("sub_dir/sub")
.file("sub_dir/sub/1.txt", "1\n")
Expand Down Expand Up @@ -1702,10 +1703,10 @@ async fn sigpipe_from_pipeline() {

#[tokio::test]
async fn shopt() {
// query all options (default: failglob on, nullglob off)
// query all options (default: failglob on, globstar on, nullglob off)
TestBuilder::new()
.command("shopt")
.assert_stdout("failglob\ton\nnullglob\toff\n")
.assert_stdout("failglob\ton\nglobstar\ton\nnullglob\toff\n")
.run()
.await;

Expand Down Expand Up @@ -1767,7 +1768,23 @@ async fn shopt() {
// multiple options
TestBuilder::new()
.command("shopt -s nullglob && shopt -u failglob && shopt")
.assert_stdout("failglob\toff\nnullglob\ton\n")
.assert_stdout("failglob\toff\nglobstar\ton\nnullglob\ton\n")
.run()
.await;

// query globstar option (on by default)
TestBuilder::new()
.command("shopt globstar")
.assert_stdout("globstar\ton\n")
.assert_exit_code(0) // returns 0 when option is on
.run()
.await;

// disable globstar
TestBuilder::new()
.command("shopt -u globstar && shopt globstar")
.assert_stdout("globstar\toff\n")
.assert_exit_code(1)
.run()
.await;
}
Expand Down Expand Up @@ -1850,25 +1867,76 @@ async fn shopt_failglob() {
.await;
}

#[tokio::test]
async fn shopt_globstar() {
// globstar on by default: ** matches zero or more directories
// use cat to verify all files are matched (content order verifies glob worked)
TestBuilder::new()
.directory("sub/deep")
.file("a.txt", "a\n")
.file("sub/b.txt", "b\n")
.file("sub/deep/c.txt", "c\n")
.command("cat **/*.txt")
.assert_stdout("a\nb\nc\n")
.assert_exit_code(0)
.run()
.await;

// single * still behaves normally even with globstar enabled
TestBuilder::new()
.directory("sub")
.file("a.txt", "a\n")
.file("sub/b.txt", "b\n")
.command("echo *.txt")
.assert_stdout("a.txt\n")
.assert_exit_code(0)
.run()
.await;

// disabling globstar: ** behaves like * (single-segment match)
TestBuilder::new()
.directory("sub/deep")
.file("a.txt", "a\n")
.file("sub/b.txt", "b\n")
.file("sub/deep/c.txt", "c\n")
.command("shopt -u globstar && cat **/*.txt")
.assert_stdout("b\n") // only matches one level deep (sub/b.txt)
.assert_exit_code(0)
.run()
.await;

// re-enabling globstar restores recursive behavior
TestBuilder::new()
.directory("sub/deep")
.file("a.txt", "a\n")
.file("sub/b.txt", "b\n")
.file("sub/deep/c.txt", "c\n")
.command("shopt -u globstar && shopt -s globstar && cat **/*.txt")
.assert_stdout("a\nb\nc\n")
.assert_exit_code(0)
.run()
.await;
}

#[tokio::test]
async fn pipefail_option() {
// Without pipefail: exit code is from last command (0)
TestBuilder::new()
.command("sh -c 'exit 1' | true")
.command("(exit 1) | true")
.assert_exit_code(0)
.run()
.await;

// With pipefail: exit code is rightmost non-zero (1)
TestBuilder::new()
.command("set -o pipefail && sh -c 'exit 1' | true")
.command("set -o pipefail && (exit 1) | true")
.assert_exit_code(1)
.run()
.await;

// Multiple failures - should return rightmost non-zero
TestBuilder::new()
.command("set -o pipefail && sh -c 'exit 2' | sh -c 'exit 3' | true")
.command("set -o pipefail && (exit 2) | (exit 3) | true")
.assert_exit_code(3)
.run()
.await;
Expand All @@ -1882,7 +1950,7 @@ async fn pipefail_option() {

// Disable pipefail with +o
TestBuilder::new()
.command("set -o pipefail && set +o pipefail && sh -c 'exit 1' | true")
.command("set -o pipefail && set +o pipefail && (exit 1) | true")
.assert_exit_code(0)
.run()
.await;
Expand Down