Skip to content

Commit

Permalink
GH-2606: A workspace project should not have the its root folder as a…
Browse files Browse the repository at this point in the history
… default source folder (#2607)

* no default source folder for workspace projects

* related adjustments

* add comment

* fix, refactor test

* added/renamed/enabled tests
  • Loading branch information
mmews-n4 authored Feb 16, 2024
1 parent cb43820 commit 8e6eab1
Show file tree
Hide file tree
Showing 10 changed files with 379 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,52 @@ public class PackageJsonHelper {
private VersionNumber cachedJSDefaultVersionNumber = null;

/**
* Transform the given {@code packageJSON} into an equivalent {@link ProjectDescriptionBuilder} instance. If no
* further adjustments are required, the client can immediately invoke the {@code #build()} method to obtain the
* corresponding {@link ProjectDescription}.
* Transform the given {@code packageJSON} into an equivalent {@link ProjectDescriptionBuilder} instance.
*
* @param packageJSON
* the JSON document to convert (should be the representation of a valid {@code package.json} file).
* @return the project description converted from the given JSON document or <code>null</code> if the root value of
* the given JSON document is not a {@link JSONObject}.
*/
public ProjectDescriptionBuilder convertToProjectDescription(JSONDocument packageJSON) {
JSONValue rootValue = packageJSON.getContent();
if (!(rootValue instanceof JSONObject)) {
return null;
}

ProjectDescriptionBuilder target = new ProjectDescriptionBuilder();
List<NameValuePair> rootPairs = ((JSONObject) rootValue).getNameValuePairs();
convertRootPairs(target, rootPairs);

return target;
}

/**
* Adjusts the given target and applies default values.
* <p>
* Note: this method does not implement the package.json feature that a "main" path may point to a folder and then a
* file "index.js" in that folder will be used as main module (for details see
* {@link ProjectDescriptionUtils#convertMainPathToModuleSpecifier(String, List)}).
*
* @param packageJSON
* the JSON document to convert (should be the representation of a valid {@code package.json} file).
* @param target
* target that will be adjusted
* @param applyDefaultValues
* whether default values should be applied to the project description after conversion.
* @param defaultProjectName
* the default project ID (will be ignored if {@code applyDefaultValues} is set to <code>false</code>.
* @return the project description converted from the given JSON document or <code>null</code> if the root value of
* the given JSON document is not a {@link JSONObject}.
*/
public ProjectDescriptionBuilder convertToProjectDescription(JSONDocument packageJSON, boolean applyDefaultValues,
String defaultProjectName) {
public ProjectDescriptionBuilder adjustAndApplyDefaults(JSONDocument packageJSON, ProjectDescriptionBuilder target,
boolean applyDefaultValues, String defaultProjectName) {

JSONValue rootValue = packageJSON.getContent();
if (!(rootValue instanceof JSONObject)) {
return null;
}

ProjectDescriptionBuilder target = new ProjectDescriptionBuilder();
List<NameValuePair> rootPairs = ((JSONObject) rootValue).getNameValuePairs();
convertRootPairs(target, rootPairs);

String valueOfPropMain = asNonEmptyStringOrNull(getProperty((JSONObject) rootValue, MAIN.name).orElse(null));
adjustProjectDescriptionAfterConversion(target, applyDefaultValues, defaultProjectName, valueOfPropMain);

Expand Down Expand Up @@ -484,7 +501,10 @@ private void applyPlainJSDefaults(ProjectDescriptionBuilder target, String defau
trimProjectExports(target.getExports(), target.getTypesVersions());
}
}
setSourceContainer(target, (String) OUTPUT.defaultValue, false);
if (!target.isWorkspaceRoot()) {
// workspace projects my contain unrelated sources hence do not use the default source folder
setSourceContainer(target, (String) OUTPUT.defaultValue, false);
}

if (mainOrTypesModulePath != null) {
if (mainOrTypesModulePath.startsWith("./")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ public ProjectDescription loadProjectDescriptionAtLocation(FileURI location, URI

adjustMainPath(location, packageJSON);
ProjectDescriptionBuilder pdbFromPackageJSON = packageJSON != null
? packageJsonHelper.convertToProjectDescription(packageJSON, true, null)
? packageJsonHelper.convertToProjectDescription(packageJSON)
: null;
if (pdbFromPackageJSON != null) {
// the order is important here:

setInformationFromFileSystem(location, pdbFromPackageJSON);
setInformationFromTSConfig(location, pdbFromPackageJSON);
setInformationFromPnpmWorkspace(location, pdbFromPackageJSON);
pdbFromPackageJSON.setLocation(location);
pdbFromPackageJSON.setRelatedRootLocation(relatedRootLocation);

packageJsonHelper.adjustAndApplyDefaults(packageJSON, pdbFromPackageJSON, true, null);
setInformationFromTSConfig(location, pdbFromPackageJSON);

ProjectDescription result = pdbFromPackageJSON.build();
return result;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ private void collectYarnWorkspaceProjects(Path yarnProjectRoot, Map<Path, Projec
collectGlobMatches(workspaceGlob, yarnProjectRoot, pdCache, memberProjects);
}
removeUnnecessaryPlainjsProjects(memberProjects, pdCache);
for (Path member : memberProjects) {
allProjectDirs.add(member);
}
allProjectDirs.addAll(memberProjects);
}

private void collectProjects(Path root, Path relatedRoot, boolean includeSubtree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ private boolean isPckjsonOfPlainJS(JSONDocument jsonDocument) {
String fileExtension = fileExtensionCalculator.getFilenameWithoutXpectExtension(uri);
boolean isPckjson = fileExtension.equals(N4JSGlobals.PACKAGE_JSON);
if (isPckjson) {
ProjectDescriptionBuilder pdb = pckjsonHelper.convertToProjectDescription(jsonDocument, true, "xyz");
ProjectDescriptionBuilder pdb = pckjsonHelper.convertToProjectDescription(jsonDocument);
return pdb != null && pdb.build().getProjectType() == ProjectType.PLAINJS;
}
return false;
Expand Down
Loading

0 comments on commit 8e6eab1

Please sign in to comment.