Skip to content

Commit 5c3bcb6

Browse files
authored
Merge pull request #12434 from ethereum/outputLocations
Output searched locations on import failure.
2 parents 91b7d5f + 6225dad commit 5c3bcb6

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

libsolidity/interface/FileReader.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,19 @@ ReadCallback::Result FileReader::readFile(string const& _kind, string const& _so
116116
for (auto const& prefix: prefixes)
117117
{
118118
boost::filesystem::path canonicalPath = normalizeCLIPathForVFS(prefix / strippedSourceUnitName, SymlinkResolution::Enabled);
119-
120119
if (boost::filesystem::exists(canonicalPath))
121120
candidates.push_back(std::move(canonicalPath));
122121
}
123122

124123
auto pathToQuotedString = [](boost::filesystem::path const& _path){ return "\"" + _path.string() + "\""; };
125124

126125
if (candidates.empty())
127-
return ReadCallback::Result{false, "File not found."};
126+
return ReadCallback::Result{
127+
false,
128+
"File not found. Searched the following locations: " +
129+
joinHumanReadable(prefixes | ranges::views::transform(pathToQuotedString), ", ") +
130+
"."
131+
};
128132

129133
if (candidates.size() >= 2)
130134
return ReadCallback::Result{
@@ -135,19 +139,26 @@ ReadCallback::Result FileReader::readFile(string const& _kind, string const& _so
135139
"."
136140
};
137141

138-
FileSystemPathSet extraAllowedPaths = {m_basePath.empty() ? "." : m_basePath};
139-
extraAllowedPaths += m_includePaths;
142+
FileSystemPathSet allowedPaths =
143+
m_allowedDirectories +
144+
decltype(allowedPaths){m_basePath.empty() ? "." : m_basePath} +
145+
m_includePaths;
140146

141147
bool isAllowed = false;
142-
for (boost::filesystem::path const& allowedDir: m_allowedDirectories + extraAllowedPaths)
148+
for (boost::filesystem::path const& allowedDir: allowedPaths)
143149
if (isPathPrefix(normalizeCLIPathForVFS(allowedDir, SymlinkResolution::Enabled), candidates[0]))
144150
{
145151
isAllowed = true;
146152
break;
147153
}
148154

149155
if (!isAllowed)
150-
return ReadCallback::Result{false, "File outside of allowed directories."};
156+
return ReadCallback::Result{
157+
false,
158+
"File outside of allowed directories. The following are allowed: " +
159+
joinHumanReadable(allowedPaths | ranges::views::transform(pathToQuotedString), ", ") +
160+
"."
161+
};
151162

152163
if (!boost::filesystem::is_regular_file(candidates[0]))
153164
return ReadCallback::Result{false, "Not a valid file."};

test/cmdlineTests/standard_yul_single_file_via_urls/output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
[
44
{
55
"component": "general",
6-
"formattedMessage": "Cannot import url (\"in.yul\"): File not found.",
7-
"message": "Cannot import url (\"in.yul\"): File not found.",
6+
"formattedMessage": "Cannot import url (\"in.yul\"): File not found. Searched the following locations: \"\".",
7+
"message": "Cannot import url (\"in.yul\"): File not found. Searched the following locations: \"\".",
88
"severity": "error",
99
"type": "IOError"
1010
},

test/solc/CommandLineInterfaceAllowPaths.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ ImportCheck checkImport(
100100
return ImportCheck::OK();
101101

102102
static regex const sourceNotFoundErrorRegex{
103-
R"(^Error \(6275\): Source ".+" not found: (.*)\.\n)"
103+
R"(^Error \(6275\): Source "[^"]+" not found: (.*)\.\n)"
104104
R"(\s*--> .*<stdin>:\d+:\d+:\n)"
105105
R"(\s*\|\n)"
106106
R"(\d+\s*\| import '.+';\n)"
@@ -110,12 +110,12 @@ ImportCheck checkImport(
110110
smatch submatches;
111111
if (!regex_match(cliResult.stderrContent, submatches, sourceNotFoundErrorRegex))
112112
return ImportCheck::Unknown("Unexpected stderr content: '" + cliResult.stderrContent + "'");
113-
if (submatches[1] != "File not found" && submatches[1] != "File outside of allowed directories")
113+
if (submatches[1] != "File not found" && !boost::starts_with(string(submatches[1]), "File outside of allowed directories"))
114114
return ImportCheck::Unknown("Unexpected error message: '" + cliResult.stderrContent + "'");
115115

116116
if (submatches[1] == "File not found")
117117
return ImportCheck::FileNotFound();
118-
else if (submatches[1] == "File outside of allowed directories")
118+
else if (boost::starts_with(string(submatches[1]), "File outside of allowed directories"))
119119
return ImportCheck::PathDisallowed();
120120
else
121121
return ImportCheck::Unknown("Unexpected error message '" + submatches[1].str() + "'");

0 commit comments

Comments
 (0)