Skip to content

fix: apply OS detection when called without an options object - #195

Open
yhuikzdtguioaert wants to merge 1 commit into
micromatch:masterfrom
yhuikzdtguioaert:fix/windows-detection-without-options
Open

fix: apply OS detection when called without an options object#195
yhuikzdtguioaert wants to merge 1 commit into
micromatch:masterfrom
yhuikzdtguioaert:fix/windows-detection-without-options

Conversation

@yhuikzdtguioaert

Copy link
Copy Markdown

Fixes #133.

Problem

The default entry point auto-detects the OS to choose a default for the windows option, but that detection is skipped when picomatch() is called without an options object. The result then depends on whether an (empty) options object is passed:

// on Windows
picomatch('foo/*.js')('foo\\bar.js');     // false  (detection skipped)
picomatch('foo/*.js', {})('foo\\bar.js'); // true   (detection applied)

Both calls should behave the same.

Cause

In index.js the guard requires options to be truthy before defaulting windows:

if (options && (options.windows === null || options.windows === undefined)) {
  options = { ...options, windows: utils.isWindows() };
}

When options is undefined the whole condition is false, so windows is never defaulted and the matcher falls back to posix behavior even on Windows.

Fix

Also apply the default when no options object is given. { ...undefined } is an empty object, so the existing spread keeps working and the caller's object is never mutated. An explicit windows: true / windows: false is still respected.

Tests

Added two cases under options.windows in test/options.js. They stub utils.isWindows so the behavior is asserted deterministically on any platform (the first case fails without this change). Full suite is green (1979 passing).

picomatch()'s default OS detection was guarded on `options` being
truthy, so it was skipped when the matcher was created without an
options object. As a result `picomatch(glob)` and `picomatch(glob, {})`
disagreed about backslash handling on Windows. Default `windows` when
no options object is given too; explicit values are still respected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

windows option disabled on windows only if there's no options object

1 participant