Skip to content

[ImpellerC] Escape spaces in depfile paths - #190985

Closed
wilyan09007 wants to merge 3 commits into
flutter:masterfrom
wilyan09007:fix/issue-190968
Closed

wilyan09007 wants to merge 3 commits into
flutter:masterfrom
wilyan09007:fix/issue-190968

Conversation

@wilyan09007

Copy link
Copy Markdown
Contributor

Compiler::CreateDepfileContents and OutputBundleDepfile join dependency paths with plain spaces, so a path containing a space reaches the consumer as two paths that do not exist. An SDK under ~/Library/Application Support puts a space in front of every bundled shader_lib include, which is why this shows up on macOS.

Both writers now go through EscapeDepfilePath. A space becomes \ , with any run of backslashes in front of it doubled, since 2N+1 backslashes plus a space read back as N backslashes plus a space. Backslashes elsewhere are left as they are: that is the encoding Ninja, flutter_tools' DepfileService and the hooks runner's parseDepFileInputs all read correctly.

Escaping is covered by unit tests in compiler_unittests.cc.

Fixes #190968

Pre-launch Checklist

@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests labels Aug 12, 2026
@wilyan09007
wilyan09007 marked this pull request as ready for review August 23, 2026 18:10

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces path escaping for depfiles via a new EscapeDepfilePath utility to ensure spaces and backslashes are correctly handled in Ninja depfiles. This utility is integrated into Compiler::GetDependencyNames, Compiler::CreateDepfileContents, and OutputBundleDepfile, and is accompanied by unit tests. The review feedback suggests optimizing EscapeDepfilePath by replacing std::stringstream with std::string to minimize allocations, and using std::move when passing vectors to JoinStrings to avoid unnecessary copies.

for (auto& target : target_names) {
target = EscapeDepfilePath(target);
}
const auto targets = JoinStrings(target_names, " ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Since JoinStrings takes its first argument by value, passing target_names as an lvalue will copy the vector and all its string elements. We can use std::move to avoid this copy.

Suggested change
const auto targets = JoinStrings(target_names, " ");
const auto targets = JoinStrings(std::move(target_names), " ");

@b-luk b-luk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The changes LGTM. Thanks, @wilyan09007.

You'll need to merge in changes from HEAD and resolve some simple merge conflicts in the test file. I'll approve after that.

Compiler::CreateDepfileContents and OutputBundleDepfile joined dependency
paths with plain spaces, so a path containing a space reached the consumer
as two paths that do not exist. Both writers now escape a space as "\ ",
doubling any run of backslashes in front of it, and leave backslashes
elsewhere in the path alone.
@b-luk b-luk added the CICD Run CI/CD label Oct 5, 2026
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 5, 2026
@b-luk b-luk added the CICD Run CI/CD label Oct 5, 2026
b-luk
b-luk previously approved these changes Oct 6, 2026
@b-luk
b-luk requested a review from gaaclarke October 6, 2026 16:47
@b-luk

b-luk commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

@gaaclarke for second review

@gaaclarke gaaclarke 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.

compiler.cc and shader_bundler.cc don't have tests. I asked the agent to sketch these out, it would look something like this. Can you try to add tests like these to get better test coverage.

    TEST(ShaderBundleTest, GenerateShaderBundleEscapesSpacesInDepfile) {
      fml::ScopedTemporaryDirectory temp_dir;
      fml::UniqueFD spaced_fd =
          fml::OpenDirectory(temp_dir.fd(), "with space",
                             /*create_if_necessary=*/true,
                             fml::FilePermission::kReadWrite);
      ASSERT_TRUE(spaced_fd.is_valid());
      auto fixture =
          flutter::testing::OpenFixtureAsMapping("flutter_gpu_unlit.frag");
      ASSERT_TRUE(fixture);
      ASSERT_TRUE(fml::WriteAtomically(spaced_fd, "unlit.frag", *fixture));
    
      const std::filesystem::path root(temp_dir.path());
      // generic_string() uses forward slashes so the JSON stays valid on Windows.
      const std::string shader_path =
          (root / "with space" / "unlit.frag").generic_string();
    
      Switches switches;
      switches.working_directory = std::make_shared<fml::UniqueFD>(
          fml::OpenDirectory(temp_dir.path().c_str(), /*create_if_necessary=*/false,
                             fml::FilePermission::kReadWrite));
      switches.shader_bundle =
          R"({"UnlitFragment": {"type": "fragment", "file": ")" + shader_path +
          R"("}})";
      switches.sl_file_name = root / "out bundle.shaderbundle";
      switches.depfile_path = "out.d";
    
      ASSERT_TRUE(GenerateShaderBundle(switches));
    
      auto depfile =
          fml::FileMapping::CreateReadOnly(*switches.working_directory, "out.d");
      ASSERT_TRUE(depfile);
      const std::string contents(
          reinterpret_cast<const char*>(depfile->GetMapping()), depfile->GetSize());
    
      EXPECT_NE(contents.find("out\\ bundle.shaderbundle: "), std::string::npos)
          << contents;
      EXPECT_NE(contents.find("with\\ space/unlit.frag"), std::string::npos)
          << contents;
      EXPECT_EQ(contents.find("with space"), std::string::npos) << contents;
    }
    TEST(CompilerTest, DepfileEscapesSpacesInTargetsAndDependencies) {
      // Mirrors an SDK under "~/Library/Application Support", where the bundled
      // shader_lib include directory has a space in its path.
      fml::ScopedTemporaryDirectory temp_dir;
      auto include_fd = std::make_shared<fml::UniqueFD>(
          fml::OpenDirectory(temp_dir.fd(), "with space",
                             /*create_if_necessary=*/true,
                             fml::FilePermission::kReadWrite));
      ASSERT_TRUE(include_fd->is_valid());
      ASSERT_TRUE(fml::WriteAtomically(
          *include_fd, "common.glsl",
          fml::DataMapping("vec4 Red() { return vec4(1.0, 0.0, 0.0, 1.0); }\n")));
    
      const std::string shader_source = R"(
        #version 460 core
        #include <common.glsl>
        out vec4 frag_color;
        void main() { frag_color = Red(); }
      )";
      auto mapping = std::make_shared<fml::DataMapping>(shader_source);
    
      SourceOptions options;
      options.source_language = SourceLanguage::kGLSL;
      options.target_platform = TargetPlatform::kRuntimeStageGLES;
      options.type = SourceType::kFragmentShader;
      options.file_name = "in dir/a.frag";
      options.entry_point_name = "main";
      // The working directory is searched before include_dirs, so keep the include
      // out of it to force resolution through the spaced include directory.
      options.working_directory = std::make_shared<fml::UniqueFD>(
          fml::OpenDirectory(temp_dir.path().c_str(), /*create_if_necessary=*/false,
                             fml::FilePermission::kRead));
      options.include_dirs.push_back(
          IncludeDir{include_fd, temp_dir.path() + "/with space"});
    
      Reflector::Options reflector_options;
      reflector_options.target_platform = TargetPlatform::kRuntimeStageGLES;
    
      Compiler compiler(mapping, options, reflector_options);
      ASSERT_TRUE(compiler.IsValid()) << compiler.GetErrorMessages();
      ASSERT_EQ(compiler.GetIncludedFileNames().size(), 1u);
    
      auto depfile = compiler.CreateDepfileContents({"out dir/a.iplr"});
      ASSERT_TRUE(depfile);
      const std::string contents(
          reinterpret_cast<const char*>(depfile->GetMapping()), depfile->GetSize());
    
      EXPECT_EQ(contents.rfind("out\\ dir/a.iplr: ", 0), 0u) << contents;
      EXPECT_NE(contents.find("with\\ space"), std::string::npos) << contents;
      EXPECT_NE(contents.find(" in\\ dir/a.frag\n"), std::string::npos) << contents;
      EXPECT_EQ(contents.find("with space"), std::string::npos) << contents;
    }

return reinterpret_cast<const char*>(path.u8string().c_str());
}

std::string EscapeDepfilePath(const std::string& 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.

this should take in a std::string_view

Adds the depfile tests gaaclarke sketched in review for
Compiler::CreateDepfileContents and GenerateShaderBundle.
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Oct 9, 2026
@b-luk b-luk added the CICD Run CI/CD label Oct 15, 2026

@b-luk b-luk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip: After you address reviewers' comments, re-request review to bring it to our attention sooner.

const std::string contents(
reinterpret_cast<const char*>(depfile->GetMapping()), depfile->GetSize());

EXPECT_EQ(contents.rfind("out\ dir/a.iplr: ", 0), 0u) << contents;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The tests are failing because you're escaping these backslashes improperly. You need "\\" rather than "\". For both this file and the other test file.

@flutter-dashboard

Copy link
Copy Markdown

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@b-luk

b-luk commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@wilyan09007 Are you still working on this change? If not, I can take it over and fork it to get it landed.

@b-luk b-luk mentioned this pull request Nov 13, 2026
11 tasks done
@b-luk

b-luk commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Closing this one for #196555

@b-luk b-luk closed this Nov 14, 2026
prasad2512 pushed a commit to prasad2512/flutter that referenced this pull request Nov 14, 2026
Forking flutter#190985 because the
original author has stopped responding.

Original description:

`Compiler::CreateDepfileContents` and `OutputBundleDepfile` join
dependency paths with plain spaces, so a path containing a space reaches
the consumer as two paths that do not exist. An SDK under
`~/Library/Application Support` puts a space in front of every bundled
`shader_lib` include, which is why this shows up on macOS.

Both writers now go through `EscapeDepfilePath`. A space becomes `\ `,
with any run of backslashes in front of it doubled, since 2N+1
backslashes plus a space read back as N backslashes plus a space.
Backslashes elsewhere are left as they are: that is the encoding Ninja,
flutter_tools' `DepfileService` and the hooks runner's
`parseDepFileInputs` all read correctly.

Escaping is covered by unit tests in `compiler_unittests.cc`.

Fixes flutter#190968

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[flutter/website]: https://github.com/flutter/website
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md

---------

Co-authored-by: William <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Impeller] impellerc writes depfile paths unescaped, breaking consumers when a path contains a space

3 participants