Skip to content

Commit 32120bb

Browse files
michaelnebelCopilot
andcommitted
C#: Reference superseding frameworks in stub projects
The dependency manager keeps only the highest version of each assembly, so package assemblies superseded by a framework are stubbed in the framework. Reference that framework from the package project. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 11bc23f commit 32120bb

2 files changed

Lines changed: 48 additions & 10 deletions

File tree

‎csharp/scripts/stubs/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ python3 make_stubs_all.py /Users/tmp/working-dir
2727
```
2828
The script contains a hardcoded list of `packages`. If a new package is needed for test purposes, it should be added to the `packages` list in the script.
2929

30-
The generated stubs require some manual changes before they are compilable.
30+
The generated `.csproj` files reference the dependency versions that were actually restored. They also reference any framework whose stubs contain types from a package. This happens when a framework supersedes the package's assemblies, as `Microsoft.AspNetCore.App` does for `Microsoft.Extensions.*`. The generated stubs might still require some manual changes before they are compilable.
3131

3232
The output stubs are found in the `[DIR]/output/stubs` folder and can be copied over to `csharp/ql/test/resources/stubs`.

‎csharp/scripts/stubs/helpers.py‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ def make_stubs(self):
162162
frameworks = set()
163163
copiedFiles = set()
164164

165+
# Map from (lower case) DLL file name to the framework whose stubs contain it.
166+
frameworkDlls = {}
167+
for pathInfo in pathInfos:
168+
segments = pathInfo.split('/')
169+
for segment in segments[:-1]:
170+
if segment.lower().endswith('.ref'):
171+
framework = segment[:-len('.ref')]
172+
if framework.lower() != 'microsoft.netcore.app':
173+
frameworkDlls[segments[-1].lower()] = framework
174+
break
175+
165176
assetsJsonFile = os.path.join(self.projectDirIn, 'obj', 'project.assets.json')
166177
with open(assetsJsonFile) as json_data:
167178
data = json.load(json_data)
@@ -171,6 +182,15 @@ def make_stubs(self):
171182
target = list(data['targets'].keys())[0]
172183
print("Found target: " + target)
173184

185+
# Map from lower case framework name to its correctly cased name.
186+
frameworkNames = {}
187+
for framework in data['project']['frameworks'].values():
188+
for name in framework.get('frameworkReferences', {}).keys():
189+
frameworkNames[name.lower()] = name
190+
for packageInfo in data['targets'][target].values():
191+
for name in packageInfo.get('frameworkReferences', []):
192+
frameworkNames[name.lower()] = name
193+
174194
# Map from package name to the version that was actually restored.
175195
resolvedVersions = {}
176196
for package in data['targets'][target].keys():
@@ -181,6 +201,7 @@ def make_stubs(self):
181201
parts = package.split('/')
182202
name = parts[0]
183203
version = parts[1]
204+
packageInfo = data['targets'][target][package]
184205
packageDir = os.path.join(stubsDir, name, version)
185206
if not os.path.exists(packageDir):
186207
os.makedirs(packageDir)
@@ -191,33 +212,50 @@ def make_stubs(self):
191212
pf.write(' <ItemGroup>\n')
192213

193214
dlls = set()
194-
if 'compile' in data['targets'][target][package]:
195-
for dll in data['targets'][target][package]['compile']:
215+
if 'compile' in packageInfo:
216+
for dll in packageInfo['compile']:
196217
dlls.add(
197218
(name + '/' + version + '/' + dll).lower())
198-
if 'runtime' in data['targets'][target][package]:
199-
for dll in data['targets'][target][package]['runtime']:
219+
if 'runtime' in packageInfo:
220+
for dll in packageInfo['runtime']:
200221
dlls.add((name + '/' + version + '/' + dll).lower())
201222

223+
stubbedDlls = set()
202224
for pathInfo in pathInfos:
203225
for dll in dlls:
204226
if pathInfo.lower().endswith(dll):
227+
stubbedDlls.add(dll)
205228
copiedFiles.add(pathInfo)
206229
shutil.copy2(pathInfos[pathInfo], packageDir)
207230

208-
if 'dependencies' in data['targets'][target][package]:
209-
for dependency in data['targets'][target][package]['dependencies'].keys():
231+
if 'dependencies' in packageInfo:
232+
for dependency in packageInfo['dependencies'].keys():
210233
depVersion = resolvedVersions.get(dependency.lower())
211234
if depVersion is None:
212-
depString = data['targets'][target][package]['dependencies'][dependency]
235+
depString = packageInfo['dependencies'][dependency]
213236
depVersion = re.search(r"(\d+\.\d+\.\d+(-[a-z]+)?)", depString).group(0)
214237
pf.write(' <ProjectReference Include="../../' +
215238
dependency + '/' + depVersion + '/' + dependency + '.csproj" />\n')
216239

217-
if 'frameworkReferences' in data['targets'][target][package]:
240+
packageFrameworks = set()
241+
if 'frameworkReferences' in packageInfo:
242+
packageFrameworks.update(packageInfo['frameworkReferences'])
243+
244+
# The DLLs of a package may be superseded by a newer version of the same
245+
# assembly in a framework (e.g. Microsoft.Extensions.* in Microsoft.AspNetCore.App).
246+
# The dependency manager used by the stub generator resolves such conflicts by
247+
# keeping only the highest version of each assembly, so stubs are only
248+
# generated for the framework's copy. The package then needs to reference
249+
# the framework to get those stubs.
250+
for dll in dlls - stubbedDlls:
251+
framework = frameworkDlls.get(dll.split('/')[-1])
252+
if framework is not None:
253+
packageFrameworks.add(frameworkNames.get(framework.lower(), framework))
254+
255+
if packageFrameworks:
218256
if not os.path.exists(frameworksDir):
219257
os.makedirs(frameworksDir)
220-
for framework in data['targets'][target][package]['frameworkReferences']:
258+
for framework in sorted(packageFrameworks):
221259
frameworks.add(framework)
222260
frameworkDir = os.path.join(
223261
frameworksDir, framework)

0 commit comments

Comments
 (0)