@@ -59,6 +59,48 @@ def combine_filename_name(filename: str, name: str) -> str:
59
59
return filename + ":" + name
60
60
61
61
62
+ def _verify_filename_existence (filename : Path , cwd : Path ) -> Path :
63
+ """
64
+ Check if the filename exist. If it does not, try multiple heuristics to find the right filename:
65
+ - Look for contracts/FILENAME
66
+ - Look for node_modules/FILENAME
67
+ - Look for node_modules/FILENAME in all the parents directories
68
+
69
+
70
+ Args:
71
+ filename (Path): filename to check
72
+ cwd (Path): directory
73
+
74
+ Raises:
75
+ InvalidCompilation: if the filename is not found
76
+
77
+ Returns:
78
+ Path: the filename
79
+ """
80
+
81
+ if filename .exists ():
82
+ return filename
83
+
84
+ if cwd .joinpath (Path ("contracts" ), filename ).exists ():
85
+ filename = cwd .joinpath ("contracts" , filename )
86
+ elif cwd .joinpath (filename ).exists ():
87
+ filename = cwd .joinpath (filename )
88
+ # how node.js loads dependencies from node_modules:
89
+ # https://nodejs.org/api/modules.html#loading-from-node_modules-folders
90
+ elif cwd .joinpath (Path ("node_modules" ), filename ).exists ():
91
+ filename = cwd .joinpath ("node_modules" , filename )
92
+ else :
93
+ for parent in cwd .parents :
94
+ if parent .joinpath (Path ("node_modules" ), filename ).exists ():
95
+ filename = parent .joinpath (Path ("node_modules" ), filename )
96
+ break
97
+
98
+ if not filename .exists ():
99
+ raise InvalidCompilation (f"Unknown file: { filename } " )
100
+
101
+ return filename
102
+
103
+
62
104
# pylint: disable=too-many-branches
63
105
def convert_filename (
64
106
used_filename : Union [str , Path ],
@@ -75,9 +117,6 @@ def convert_filename(
75
117
crytic_compile (CryticCompile): Associated CryticCompile object
76
118
working_dir (Optional[Union[str, Path]], optional): Working directory. Defaults to None.
77
119
78
- Raises:
79
- InvalidCompilation: [description]
80
-
81
120
Returns:
82
121
Filename: Filename converted
83
122
"""
@@ -91,9 +130,9 @@ def convert_filename(
91
130
else :
92
131
filename = Path (filename_txt )
93
132
133
+ # cwd points to the directory to be used
94
134
if working_dir is None :
95
135
cwd = Path .cwd ()
96
- working_dir = cwd
97
136
else :
98
137
working_dir = Path (working_dir )
99
138
if working_dir .is_absolute ():
@@ -107,23 +146,7 @@ def convert_filename(
107
146
except ValueError :
108
147
pass
109
148
110
- if not filename .exists ():
111
- if cwd .joinpath (Path ("contracts" ), filename ).exists ():
112
- filename = cwd .joinpath ("contracts" , filename )
113
- elif working_dir .joinpath (filename ).exists ():
114
- filename = working_dir .joinpath (filename )
115
- # how node.js loads dependencies from node_modules:
116
- # https://nodejs.org/api/modules.html#loading-from-node_modules-folders
117
- elif cwd .joinpath (Path ("node_modules" ), filename ).exists ():
118
- filename = cwd .joinpath ("node_modules" , filename )
119
- else :
120
- for parent in cwd .parents :
121
- if parent .joinpath (Path ("node_modules" ), filename ).exists ():
122
- filename = parent .joinpath (Path ("node_modules" ), filename )
123
- break
124
-
125
- if not filename .exists ():
126
- raise InvalidCompilation (f"Unknown file: { filename } " )
149
+ filename = _verify_filename_existence (filename , cwd )
127
150
128
151
if not filename .is_absolute ():
129
152
filename = cwd .joinpath (filename )
@@ -133,10 +156,10 @@ def convert_filename(
133
156
134
157
# Build the short path
135
158
try :
136
- if working_dir .is_absolute ():
137
- short = absolute .relative_to (working_dir )
159
+ if cwd .is_absolute ():
160
+ short = absolute .relative_to (cwd )
138
161
else :
139
- short = relative .relative_to (working_dir )
162
+ short = relative .relative_to (cwd )
140
163
except ValueError :
141
164
short = relative
142
165
except RuntimeError :
0 commit comments