Skip to content

Commit 891da14

Browse files
committed
Relax Migration check for package.json in root
1 parent 682a1c3 commit 891da14

2 files changed

Lines changed: 109 additions & 13 deletions

File tree

docs/Core/Migration-Checks.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ When you build an Electron.NET project, the following validation checks are perf
88

99
| Code | Check | Description |
1010
|------|-------|-------------|
11-
| [ELECTRON001](#1-packagejson-not-allowed) | package.json not allowed | Ensures no package.json exists outside ElectronHostHook |
11+
| [ELECTRON001](#1-packagejson-rules) | package.json location rules | Ensures `package.json`/`package-lock.json` aren’t present in unsupported locations (root `package.json` handled separately) |
12+
| [ELECTRON008](#1-packagejson-rules) | root package.json contains electron | Warns when root `package.json` contains the word `electron` (case-insensitive) |
13+
| [ELECTRON009](#1-packagejson-rules) | root package.json copied to output | Warns when root `package.json` is configured to be copied to output/publish |
1214
| [ELECTRON002](#2-electron-manifestjson-not-allowed) | electron-manifest.json not allowed | Detects deprecated manifest files |
1315
| [ELECTRON003](#3-electron-builderjson-location) | electron-builder.json location | Verifies electron-builder.json exists in Properties folder |
1416
| [ELECTRON004](#3-electron-builderjson-location) | electron-builder.json wrong location | Warns if electron-builder.json is found in incorrect locations |
@@ -18,24 +20,38 @@ When you build an Electron.NET project, the following validation checks are perf
1820

1921
---
2022

21-
## 1. package.json not allowed
23+
## 1. package.json rules
2224

23-
**Warning Code:** `ELECTRON001`
25+
**Warning Codes:** `ELECTRON001`, `ELECTRON008`, `ELECTRON009`
2426

2527
### What is checked
2628

27-
The build system scans for `package.json` and `package-lock.json` files in your project directory. These files should not exist in the project root or subdirectories (with one exception).
29+
The build system scans for `package.json` and `package-lock.json` files in your project directory.
30+
31+
Rules:
32+
33+
- **ELECTRON001**: `package.json` / `package-lock.json` must not exist in the project directory or subdirectories
34+
- Exception: `ElectronHostHook` folder is allowed
35+
- Note: a **root** `package.json` is **excluded** from `ELECTRON001` and validated by `ELECTRON008` / `ELECTRON009`
36+
37+
- **ELECTRON008**: If a root `package.json` exists, it must **not** contain electron-related dependencies or configuration.
38+
39+
- **ELECTRON009**: If a root `package.json` exists, it must **not** be configured to be copied to output/publish (for example via `CopyToOutputDirectory` / `CopyToPublishDirectory` metadata)
2840

2941
### Why this matters
3042

31-
In previous versions of Electron.NET, a `package.json` file was required in the project. The new version generates this file automatically from MSBuild properties defined in your `.csproj` file.
43+
Electron.NET generates its Electron-related `package.json` during publishing based on MSBuild properties. A user-maintained Electron-related `package.json` can conflict with that process.
44+
45+
Also, ensuring the root `package.json` is not copied prevents accidentally shipping it with the published app.
3246

3347
### Exception
3448

35-
A `package.json` file **is allowed** in the `ElectronHostHook` folder if you're using custom host hooks. This is the only valid location for a manually maintained package.json.
49+
A `package.json` / `package-lock.json` file **is allowed** in the `ElectronHostHook` folder if you're using custom host hooks.
3650

3751
### How to fix
3852

53+
If you have an Electron-related `package.json` from older Electron.NET versions:
54+
3955
1. **Open your project's `.csproj` file**
4056
2. **Add the required properties** to a PropertyGroup with the label `ElectronNetCommon`:
4157

@@ -51,7 +67,12 @@ A `package.json` file **is allowed** in the `ElectronHostHook` folder if you're
5167
</PropertyGroup>
5268
```
5369

54-
3. **Delete the old `package.json`** file from your project root
70+
3. **Delete** Electron-related `package.json` / `package-lock.json` files (except those under `ElectronHostHook` if applicable)
71+
72+
If you keep a root `package.json` for non-Electron reasons:
73+
74+
- Ensure it does **not** contain electron dependencies or configuration (fixes `ELECTRON008`)
75+
- Ensure it is **not** copied to output/publish (fixes `ELECTRON009`)
5576

5677
> **See also:** [Migration Guide](Migration-Guide.md) for complete migration instructions.
5778

src/ElectronNET/build/ElectronNET.MigrationChecks.targets

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<PropertyGroup>
55
<ElectronMigrationChecksDependsOn>
66
ElectronCheckNoPackageJson;
7+
ElectronCheckRootPackageJsonNoElectron;
8+
ElectronCheckRootPackageJsonNotCopied;
79
ElectronCheckNoManifestJson;
810
ElectronCheckElectronBuilderJson;
911
ElectronCheckNoParentPaths;
@@ -19,14 +21,17 @@
1921
</Target>
2022

2123
<!--
22-
Check 1: No package.json must be present in the project (except ElectronHostHook folder)
24+
Check 1: No package.json/package-lock.json must be present in the project (except ElectronHostHook folder)
25+
26+
NOTE: Root package.json is excluded from ELECTRON001 and checked by separate targets.
2327
-->
2428
<Target Name="ElectronCheckNoPackageJson">
2529

26-
<!-- Find all package.json files, excluding ElectronHostHook folder and output directories -->
30+
<!-- Find all package.json files, excluding ElectronHostHook folder, root, and output directories -->
2731
<ItemGroup>
2832
<_InvalidPackageJson Include="$(MSBuildProjectDirectory)\**\package.json"
29-
Exclude="$(MSBuildProjectDirectory)\ElectronHostHook\**\package.json;
33+
Exclude="$(MSBuildProjectDirectory)\package.json;
34+
$(MSBuildProjectDirectory)\ElectronHostHook\**\package.json;
3035
$(MSBuildProjectDirectory)\bin\**\package.json;
3136
$(MSBuildProjectDirectory)\obj\**\package.json;
3237
$(MSBuildProjectDirectory)\publish\**\package.json;
@@ -46,17 +51,87 @@
4651

4752
<Warning Condition="'$(_HasInvalidPackageJson)' == 'true'"
4853
Code="ELECTRON001"
49-
Text="Found package.json or package-lock.json file(s) in the project root or subdirectories. These files are no longer supported in this location.
54+
Text="Found package.json or package-lock.json file(s) in unsupported location(s). These files are no longer supported in this location.
5055
5156
Files found:
5257
@(_InvalidPackageJson, '%0A')@(_InvalidPackageLockJson, '%0A')
5358
5459
MIGRATION REQUIRED:
55-
All properties from an existing package.json file must now be specified as MSBuild properties in the project file.
60+
All Electron.NET-related properties from an existing package.json must now be specified as MSBuild properties in the project file.
5661
5762
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#1-packagejson-not-allowed
5863
59-
EXCEPTION: package.json and package-lock.json files ARE allowed in the 'ElectronHostHook' folder for custom host hook implementations." />
64+
EXCEPTION:
65+
- package.json and package-lock.json files ARE allowed in the 'ElectronHostHook' folder for custom host hook implementations.
66+
- A package.json in the project root is handled by separate migration checks." />
67+
68+
</Target>
69+
70+
<!--
71+
Check 1b: Root package.json must not contain Electron-related configuration
72+
-->
73+
<Target Name="ElectronCheckRootPackageJsonNoElectron"
74+
Condition="Exists('$(MSBuildProjectDirectory)\package.json')">
75+
76+
<ItemGroup>
77+
<_RootPackageJsonLines Include="$([System.IO.File]::ReadAllLines('$(MSBuildProjectDirectory)\package.json'))" />
78+
</ItemGroup>
79+
80+
<PropertyGroup>
81+
<_RootPackageJsonContent>@(_RootPackageJsonLines, ' ')</_RootPackageJsonContent>
82+
<_RootPackageJsonHasElectron>false</_RootPackageJsonHasElectron>
83+
<_RootPackageJsonHasElectron Condition="$([System.Text.RegularExpressions.Regex]::IsMatch('$(_RootPackageJsonContent)', 'electron', System.Text.RegularExpressions.RegexOptions.IgnoreCase))">true</_RootPackageJsonHasElectron>
84+
</PropertyGroup>
85+
86+
<Warning
87+
Condition="'$(_RootPackageJsonHasElectron)' == 'true'"
88+
Code="ELECTRON008"
89+
Text="The project contains a root package.json that references 'electron' (case-insensitive).
90+
91+
File:
92+
$(MSBuildProjectDirectory)\package.json
93+
94+
MIGRATION REQUIRED:
95+
Electron.NET configuration must be defined via MSBuild properties and electron-builder.json (not via a user-provided package.json).
96+
97+
HOW TO FIX:
98+
- Remove Electron-related entries from the root package.json, or delete the file if it's only used for Electron configuration.
99+
100+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#1-packagejson-not-allowed" />
101+
102+
</Target>
103+
104+
<!--
105+
Check 1c: Root package.json must not be copied to output/publish
106+
-->
107+
<Target Name="ElectronCheckRootPackageJsonNotCopied"
108+
Condition="Exists('$(MSBuildProjectDirectory)\package.json')">
109+
110+
<ItemGroup>
111+
<_RootPackageJsonFile Include="@(Content);@(None)"
112+
Condition="'%(Identity)' == 'package.json' OR '%(Identity)' == '$(MSBuildProjectDirectory)\package.json'" />
113+
</ItemGroup>
114+
115+
<PropertyGroup>
116+
<_RootPackageJsonIsCopied>false</_RootPackageJsonIsCopied>
117+
<_RootPackageJsonIsCopied Condition="'@(_RootPackageJsonFile)' != '' AND ( '%(_RootPackageJsonFile.CopyToOutputDirectory)' != '' OR '%(_RootPackageJsonFile.CopyToPublishDirectory)' != '' )">true</_RootPackageJsonIsCopied>
118+
</PropertyGroup>
119+
120+
<Warning
121+
Condition="'$(_RootPackageJsonIsCopied)' == 'true'"
122+
Code="ELECTRON009"
123+
Text="The project contains a root package.json that is configured to be copied to the output/publish directory.
124+
125+
File:
126+
$(MSBuildProjectDirectory)\package.json
127+
128+
MIGRATION REQUIRED:
129+
Root package.json must not be copied during build/publish.
130+
131+
HOW TO FIX:
132+
- Remove CopyToOutputDirectory / CopyToPublishDirectory metadata for package.json in your project file.
133+
134+
For more information, see: https://github.com/ElectronNET/Electron.NET/wiki/Migration-Checks#1-packagejson-not-allowed" />
60135

61136
</Target>
62137

0 commit comments

Comments
 (0)