@@ -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 ():
@@ -106,27 +145,21 @@ def convert_filename(
106
145
filename = filename .relative_to (Path (crytic_compile .package_name ))
107
146
except ValueError :
108
147
pass
109
- if not filename .exists ():
110
- if cwd .joinpath (Path ("node_modules" ), filename ).exists ():
111
- filename = cwd .joinpath ("node_modules" , filename )
112
- elif cwd .joinpath (Path ("contracts" ), filename ).exists ():
113
- filename = cwd .joinpath ("contracts" , filename )
114
- elif working_dir .joinpath (filename ).exists ():
115
- filename = working_dir .joinpath (filename )
116
- else :
117
- raise InvalidCompilation (f"Unknown file: { filename } " )
118
- elif not filename .is_absolute ():
148
+
149
+ filename = _verify_filename_existence (filename , cwd )
150
+
151
+ if not filename .is_absolute ():
119
152
filename = cwd .joinpath (filename )
120
153
121
154
absolute = filename
122
155
relative = Path (os .path .relpath (filename , Path .cwd ()))
123
156
124
157
# Build the short path
125
158
try :
126
- if working_dir .is_absolute ():
127
- short = absolute .relative_to (working_dir )
159
+ if cwd .is_absolute ():
160
+ short = absolute .relative_to (cwd )
128
161
else :
129
- short = relative .relative_to (working_dir )
162
+ short = relative .relative_to (cwd )
130
163
except ValueError :
131
164
short = relative
132
165
except RuntimeError :
0 commit comments