Skip to content

Enable batchfiles to run via CreateProcessW - #30490

Closed
ahewitson-ops wants to merge 6 commits into
bazelbuild:masterfrom
ahewitson-ops:batchfile_longpaths
Closed

ahewitson-ops wants to merge 6 commits into
bazelbuild:masterfrom
ahewitson-ops:batchfile_longpaths

Conversation

@ahewitson-ops

@ahewitson-ops ahewitson-ops commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Enables Batchfiles to be ran via CreateProcessW even when their paths exceed MAX_PATH when running in tw.exe.

Looks to fix #30431

Motivation

#29921 helped us Windows users by enabling long paths for all .exe files, but specifically left out Batch/cmd files. Specifically because .bat and .cmd files must be run through an interpreter like cmd.exe, as stated in the CreateProcessW documentation.

However, testing shows that CreateProcessW can in fact handle being given a .bat or .cmd file above long paths, and silently rewrites the call to shunt it over to cmd.exe without the user having to do anything. The only caveat is that cmd.exe does not understand the \\?\ prefix for long paths and so a .bat/.cmd path must be passed without it.

I want to also note the manifest changes came from @nwatkiss in another PR for handling long paths HERE.

Note that this only works in places where a longpathAware manifest is applied to the binary.

Build API Changes

No

Checklist

  • I have added tests for the new use cases (if any).
  • I have updated the documentation (if applicable).

Release Notes

RELNOTES: Allows Batchfiles ran by CreateProcessW in tw.exe to work under longpathAware.

@ahewitson-ops
ahewitson-ops marked this pull request as ready for review July 28, 2026 14:29
@github-actions github-actions Bot added area-Windows Windows-specific issues and feature requests awaiting-review PR is awaiting review from an assigned reviewer labels Jul 28, 2026
@ahewitson-ops

Copy link
Copy Markdown
Contributor Author

As Marc pointed out in the Windows channel, in theory this could be working but slow because windows first tries to run it as a PE binary and then silently rewrites to cmd.exe when that fails.

@iancha1992 iancha1992 added the team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website label Jul 28, 2026
@fmeum
fmeum requested a review from meteorcloudy August 10, 2026 11:41
@bazel-docs-pr-commenter

bazel-docs-pr-commenter Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

@meteorcloudy

Copy link
Copy Markdown
Member

Enable Batchfiles to also be ran when their paths exceed MAX_PATH.

Do I understand correctly this only works under tw.exe because we have enabled longpathAware in the mainifest file? For example, this wouldn't work in process_test.exe or bazel.exe since they don't have longpathAware enabled.

@fmeum

fmeum commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

What matters for Bazel is the manifest of the embedded java.exe, which we already patch extensively. We could make it long path aware too, but we would need to check whether the JDK has issues with that.

@meteorcloudy

Copy link
Copy Markdown
Member

I'm fine with fixing tw.exe only, but we should be clear in the PR description about what are the actual conditions for this to work

@ahewitson-ops

ahewitson-ops commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Enable Batchfiles to also be ran when their paths exceed MAX_PATH.

Do I understand correctly this only works under tw.exe because we have enabled longpathAware in the mainifest file? For example, this wouldn't work in process_test.exe or bazel.exe since they don't have longpathAware enabled.

Using long paths in general? Yes, it's only because we have a manifest file for tw.exe that enables longpathAware so any executable without that cannot use long paths. If we wanted to make process_test or bazel longpath aware then yeah we'd have to make sure they have a manifest as well.

I've updated the PR description a little and the release notes.

@meteorcloudy

Copy link
Copy Markdown
Member

I believe the host Windows machine also has to enable LongPathsEnabled = 1 in the Windows registry?

@ahewitson-ops

Copy link
Copy Markdown
Contributor Author

I believe the host Windows machine also has to enable LongPathsEnabled = 1 in the Windows registry?

Yes, which I believe is the default nowadays for Windows machines. But this PR doesn't really change the need for that registry key, as it was already added in #29921

Comment thread src/main/native/windows/util.cc Outdated
@@ -314,14 +314,18 @@ wstring AsExecutablePathForCreateProcess(wstring path, wstring* quoted_path,
// lpApplicationName: it is not subject to MAX_PATH, and providing it lifts
// that limit from the executable part of CreateProcessW's lpCommandLine too.
// This works only for a plain executable with an absolute, normalized path.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we also update comment here?

Comment thread src/test/native/windows/process_test.cc Outdated
std::wstring proc_error;
if (!proc.Create(bat_path, L"", nullptr, L".", devnull, pipe_write,
stderr_dup, nullptr, &proc_error)) {
GTEST_SKIP() << "WaitableProcess::Create failed: "

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be:

ASSERT_TRUE(proc.Create(bat_path, L"", nullptr, L".", devnull, pipe_write,
                        stderr_dup, nullptr, &proc_error))
    << blaze_util::WstringToCstring(proc_error);

?

@meteorcloudy meteorcloudy self-assigned this Sep 16, 2026
@meteorcloudy meteorcloudy added awaiting-user-response Awaiting a response from the author and removed awaiting-review PR is awaiting review from an assigned reviewer labels Sep 17, 2026
@meteorcloudy meteorcloudy added copybara:import Trigger copybara to import the PR and removed awaiting-user-response Awaiting a response from the author labels Sep 18, 2026

@meteorcloudy meteorcloudy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Windows Windows-specific issues and feature requests copybara:import Trigger copybara to import the PR team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: Batch files cannot be path shortened enough

4 participants