Report .gn errors instead of crashing Setup::FillOtherConfig() printed three of its errors itself instead of returning them: a missing buildconfig, an invalid ninja_required_version, and both check_targets and no_check_targets being set. Its callers then crashed printing the empty error. For example, "gn gen" and "gn edit" crashed when the .gn file had no buildconfig. Return them like its other errors. Also add a CreateSourceRoot() helper for the many setup tests that create a temporary source root with a .gn file and a BUILDCONFIG.gn, and use it in all of them. Fixed: 432721745 Change-Id: Iec77a848befe45a6d3c75352f00348584215cb95 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/28580 Reviewed-by: David Turner <digit@google.com> Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: Takuto Ikuta <tikuta@google.com>
diff --git a/src/gn/setup.cc b/src/gn/setup.cc index 6abf641..71033c6 100644 --- a/src/gn/setup.cc +++ b/src/gn/setup.cc
@@ -1087,9 +1087,9 @@ std::optional<Version> version = Version::FromString(ninja_required_version_value->string_value()); if (!version) { - Err(Location(), "Invalid Ninja version '" + - ninja_required_version_value->string_value() + "'") - .PrintToStdout(); + *err = Err(Location(), "Invalid Ninja version '" + + ninja_required_version_value->string_value() + + "'"); return false; } build_settings_.set_ninja_required_version(*version); @@ -1184,11 +1184,10 @@ const Value* build_config_value = dotfile_scope_.GetValue("buildconfig", true); if (!build_config_value) { - Err(Location(), "No build config file.", - "Your .gn file (\"" + FilePathToUTF8(dotfile_name_) + - "\")\n" - "didn't specify a \"buildconfig\" value.") - .PrintToStdout(); + *err = Err(Location(), "No build config file.", + "Your .gn file (\"" + FilePathToUTF8(dotfile_name_) + + "\")\n" + "didn't specify a \"buildconfig\" value."); return false; } else if (!build_config_value->VerifyTypeIs(Value::STRING, err)) { return false; @@ -1213,12 +1212,11 @@ dotfile_scope_.GetValue("no_check_targets", true); if (no_check_targets_value) { if (check_targets_value) { - Err(Location(), "Conflicting check settings.", - "Your .gn file (\"" + FilePathToUTF8(dotfile_name_) + - "\")\n" - "specified both check_targets and no_check_targets and at most " - "one is allowed.") - .PrintToStdout(); + *err = Err(Location(), "Conflicting check settings.", + "Your .gn file (\"" + FilePathToUTF8(dotfile_name_) + + "\")\n" + "specified both check_targets and no_check_targets and " + "at most one is allowed."); return false; } no_check_patterns_ = std::make_unique<std::vector<LabelPattern>>();
diff --git a/src/gn/setup_unittest.cc b/src/gn/setup_unittest.cc index 648eb13..a125ef9 100644 --- a/src/gn/setup_unittest.cc +++ b/src/gn/setup_unittest.cc
@@ -4,6 +4,8 @@ #include "gn/setup.h" +#include <string_view> + #include "base/command_line.h" #include "base/files/file_path.h" #include "base/files/file_util.h" @@ -16,13 +18,41 @@ #include "util/build_config.h" #include "util/msg_loop.h" +namespace { + using SetupTest = TestWithScheduler; -static void WriteFile(const base::FilePath& file, const std::string& data) { +// A .gn file that only sets the build config. +constexpr char kMinimalDotfile[] = R"( +buildconfig = "//BUILDCONFIG.gn" +)"; + +// A BUILDCONFIG.gn that makes //:toolchain the default toolchain. +constexpr char kToolchainBuildConfig[] = R"( +set_default_toolchain("//:toolchain") +)"; + +} // namespace + +static void WriteFile(const base::FilePath& file, std::string_view data) { CHECK_EQ(static_cast<int>(data.size()), // Way smaller than INT_MAX. base::WriteFile(file, data.data(), data.size())); } +// Creates a source root in `temp_dir`, with a .gn file with `dotfile_contents` +// and a BUILDCONFIG.gn with `buildconfig_contents`, and returns its path. +static base::FilePath CreateSourceRoot( + base::ScopedTempDir* temp_dir, + std::string_view dotfile_contents = kMinimalDotfile, + std::string_view buildconfig_contents = "") { + CHECK(temp_dir->CreateUniqueTempDir()); + base::FilePath path = base::MakeAbsoluteFilePath(temp_dir->GetPath()); + WriteFile(path.Append(FILE_PATH_LITERAL(".gn")), dotfile_contents); + WriteFile(path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), + buildconfig_contents); + return path; +} + static std::string ReadFile(const base::FilePath& file) { std::string data; CHECK(base::ReadFileToString(file, &data)); @@ -35,11 +65,8 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir); base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, "buildconfig = \"//BUILDCONFIG.gn\"\n"); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); cmdline.AppendSwitchPath(switches::kRoot, in_path); // Create another temp dir for writing the generated files to. @@ -66,12 +93,7 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitchPath(switches::kRoot, in_path); // Create another temp dir for writing the generated files to. @@ -97,12 +119,7 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitchPath(switches::kRoot, in_path); // Create another temp dir for writing the generated files to. @@ -126,14 +143,11 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, - "buildconfig = \"//BUILDCONFIG.gn\"\n\ + base::FilePath in_path = + CreateSourceRoot(&in_temp_dir, + "buildconfig = \"//BUILDCONFIG.gn\"\n\ build_file_extension = \"" + - extension + "\""); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + extension + "\""); cmdline.AppendSwitchPath(switches::kRoot, in_path); // Create another temp dir for writing the generated files to. @@ -164,6 +178,46 @@ RunExtensionCheckTest("yay", true, ""); } +// Errors in the .gn file must be returned through `err`, since callers print +// it when setup fails. +static void RunDotfileErrorTest(std::string_view dotfile_contents, + std::string_view expected_message) { + base::CommandLine cmdline(base::CommandLine::NO_PROGRAM); + + base::ScopedTempDir in_temp_dir; + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, dotfile_contents); + cmdline.AppendSwitchPath(switches::kRoot, in_path); + + base::ScopedTempDir build_temp_dir; + ASSERT_TRUE(build_temp_dir.CreateUniqueTempDir()); + + Setup setup; + Err err; + EXPECT_FALSE(setup.DoSetupWithErr(FilePathToUTF8(build_temp_dir.GetPath()), + true, cmdline, &err)); + ASSERT_TRUE(err.has_error()); + EXPECT_EQ(expected_message, err.message()); +} + +TEST_F(SetupTest, MissingBuildConfig) { + RunDotfileErrorTest("", "No build config file."); +} + +TEST_F(SetupTest, InvalidNinjaRequiredVersion) { + RunDotfileErrorTest( + "buildconfig = \"//BUILDCONFIG.gn\"\n" + "ninja_required_version = \"x\"\n", + "Invalid Ninja version 'x'"); +} + +TEST_F(SetupTest, ConflictingCheckSettings) { + RunDotfileErrorTest( + "buildconfig = \"//BUILDCONFIG.gn\"\n" + "check_targets = []\n" + "no_check_targets = []\n", + "Conflicting check settings."); +} + TEST_F(SetupTest, AddExportCompileCommands) { base::CommandLine cmdline(base::CommandLine::NO_PROGRAM); @@ -175,12 +229,7 @@ // Create a temp directory containing the build. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitch(switches::kRoot, FilePathToUTF8(in_path)); // Two additions to the compile commands list. @@ -221,10 +270,7 @@ // Create a temp directory containing the build. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), kDotfileContents); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitch(switches::kRoot, FilePathToUTF8(in_path)); // Create another temp dir for writing the generated files to. @@ -256,12 +302,7 @@ // Create a temp directory containing the build. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), kDotfileContents); - + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitch(switches::kRoot, FilePathToUTF8(in_path)); // Create another temp dir for writing the generated files to. @@ -291,12 +332,7 @@ // Create a temp directory containing the build. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), kDotfileContents); - + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitch(switches::kRoot, FilePathToUTF8(in_path)); // Override the default root pattern list. @@ -356,13 +392,10 @@ // Create a temp directory containing the build. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); + base::FilePath in_path = + CreateSourceRoot(&in_temp_dir, kDotfileContents, kBuildConfigContents); WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILD.gn")), kBuildGnContents); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), - kBuildConfigContents); - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), kDotfileContents); cmdline.AppendSwitch(switches::kRoot, FilePathToUTF8(in_path)); @@ -419,12 +452,7 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitchPath(switches::kRoot, in_path); WriteFile(in_path.Append(FILE_PATH_LITERAL("build_defines.gni")), @@ -469,16 +497,12 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); base::FilePath absolute_bin_dir = in_path.Append(FILE_PATH_LITERAL("bin")); base::FilePath script_executable = absolute_bin_dir.Append(FILE_PATH_LITERAL("python")); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); cmdline.AppendSwitchPath(switches::kRoot, in_path); cmdline.AppendSwitchPath(switches::kScriptExecutable, script_executable); @@ -506,10 +530,7 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); base::ScopedTempDir other_temp_dir; ASSERT_TRUE(other_temp_dir.CreateUniqueTempDir()); @@ -518,7 +539,6 @@ base::FilePath script_executable = absolute_bin_dir.Append(FILE_PATH_LITERAL("python")); - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); cmdline.AppendSwitchPath(switches::kRoot, in_path); cmdline.AppendSwitchPath(switches::kScriptExecutable, script_executable); @@ -546,10 +566,7 @@ // Create a temp directory containing a .gn file and a BUILDCONFIG.gn file, // pass it as --root. base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); base::FilePath relative_bin_dir(FILE_PATH_LITERAL("bin")); base::FilePath script_executable = @@ -575,7 +592,6 @@ base::SetCurrentDirectory(cwd_temp_dir.GetPath()); #endif - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); cmdline.AppendSwitchPath(switches::kRoot, in_path); cmdline.AppendSwitchPath(switches::kScriptExecutable, script_executable); @@ -609,12 +625,7 @@ )"; base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitchPath(switches::kRoot, in_path); base::FilePath build_dir = in_path.Append(FILE_PATH_LITERAL("out")) @@ -646,12 +657,7 @@ )"; base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - base::FilePath dot_gn_name = in_path.Append(FILE_PATH_LITERAL(".gn")); - WriteFile(dot_gn_name, kDotfileContents); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), ""); + base::FilePath in_path = CreateSourceRoot(&in_temp_dir, kDotfileContents); cmdline.AppendSwitchPath(switches::kRoot, in_path); base::FilePath build_dir = in_path.Append(FILE_PATH_LITERAL("out")) @@ -721,14 +727,8 @@ ScopedCommandLineForTest global_cmdline; base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), - "buildconfig = \"//BUILDCONFIG.gn\"\n"); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), - "set_default_toolchain(\"//:toolchain\")\n"); + base::FilePath in_path = + CreateSourceRoot(&in_temp_dir, kMinimalDotfile, kToolchainBuildConfig); std::string build_gn = R"( toolchain("toolchain") { @@ -822,14 +822,8 @@ ScopedCommandLineForTest global_cmdline; base::ScopedTempDir in_temp_dir; - ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir()); - base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath()); - - WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")), - "buildconfig = \"//BUILDCONFIG.gn\"\n"); - - WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")), - "set_default_toolchain(\"//:toolchain\")\n"); + base::FilePath in_path = + CreateSourceRoot(&in_temp_dir, kMinimalDotfile, kToolchainBuildConfig); std::string build_gn = R"( toolchain("toolchain") {