Skip to content

Commit

Permalink
[GR-58394] Fix Eclipse config generation with absolute paths, on Wind…
Browse files Browse the repository at this point in the history
…ows, and provide alternative entry point for VSCode usage

PullRequest: mx/1838
  • Loading branch information
timfel committed Sep 23, 2024
2 parents d3d5f3a + 571dd66 commit a090938
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 14 deletions.
18 changes: 9 additions & 9 deletions common.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"Jsonnet files should not include this file directly but use ci/common.jsonnet instead."
],

"mx_version": "7.29.6",
"mx_version": "7.31.0",

"COMMENT.jdks": "When adding or removing JDKs keep in sync with JDKs in ci/common.jsonnet",
"jdks": {
"galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+14-1507", "platformspecific": true, "extrabundles": ["static-libs"]},
"galahad-jdk": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+15-1637", "platformspecific": true, "extrabundles": ["static-libs"]},

"oraclejdk11": {"name": "jpg-jdk", "version": "11.0.11", "build_id": "jdk-11.0.11+9", "platformspecific": true, "extrabundles": ["static-libs"] },

Expand Down Expand Up @@ -45,13 +45,13 @@
"labsjdk-ee-21-llvm": {"name": "labsjdk", "version": "ee-21.0.2+13-jvmci-23.1-b33-sulong", "platformspecific": true },
"graalvm-ee-21": {"name": "graalvm-java21", "version": "23.1.3", "platformspecific": true },

"oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+14", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+14-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+14-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+14-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+14-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+14-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+14-jvmci-b01-sulong", "platformspecific": true }
"oraclejdk-latest": {"name": "jpg-jdk", "version": "24", "build_id": "jdk-24+15", "platformspecific": true, "extrabundles": ["static-libs"]},
"labsjdk-ce-latest": {"name": "labsjdk", "version": "ce-24+15-jvmci-b01", "platformspecific": true },
"labsjdk-ce-latestDebug": {"name": "labsjdk", "version": "ce-24+15-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ce-latest-llvm": {"name": "labsjdk", "version": "ce-24+15-jvmci-b01-sulong", "platformspecific": true },
"labsjdk-ee-latest": {"name": "labsjdk", "version": "ee-24+15-jvmci-b01", "platformspecific": true },
"labsjdk-ee-latestDebug": {"name": "labsjdk", "version": "ee-24+15-jvmci-b01-debug", "platformspecific": true },
"labsjdk-ee-latest-llvm": {"name": "labsjdk", "version": "ee-24+15-jvmci-b01-sulong", "platformspecific": true }
},

"eclipse": {
Expand Down
46 changes: 46 additions & 0 deletions docs/IDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Once you have installed Eclipse, if you have multiple Java versions on your comp
```

Run `mx eclipseinit` to create the Eclipse project configurations.
You may set the `WORKSPACE` environment variable to an Eclipse workspace directory, otherwise the workspace is expected to be a parent of the primary suite.
This will print the following instructions on how to import projects:

```
Expand Down Expand Up @@ -178,3 +179,48 @@ In the same setting window, a `black` executable with the correct version should
the [Style Guide](./Styleguide.md) for more information.

To auto-format on save, select `Auto-format editor contents before saving?` under `Window > PyDev > Editor > Save Actions`.

### VSCode
This section describes how to set up VSCode for development.

VSCode is supported via the Eclipse-based [Language Support for Java](https://marketplace.visualstudio.com/items?itemName=redhat.java).
Follow the instructions of the plugin to set it up first.

Run `mx vscodeinit` to create the project configurations.
This generates Eclipse project configurations and a `.code-workspace` file to open in VSCode.
It will print instructions about how to import and which workspace file to open:

```
----------------------------------------------
VSCode project generation successfully completed for /home/dev/graalpython.code-workspace
The recommended next steps are:
1) Run mx build. This ensures all shaded JARs and annotation processors are built.
2) Open VSCode.
3) Make sure you have installed the 'Language Support for Java' extension.
4) Open /home/dev/graalpython.code-workspace as workspace.
Note that setting MX_BUILD_EXPLODED=true can improve build times. See "Exploded builds" in the mx README.md.
----------------------------------------------
```

Use `File` > `Open Workspace from File...` and select the `.code-workspace` file.

> After updating your sources and re-running `mx vscodeinit`, new projects may be created and old ones removed. This usually results in an error message indicating that a project is missing another required Java project. To handle this, you simply need repeat the steps above for importing projects.
In order to debug with VSCode, you should launch using the `-d` global option.

#### Experimental parallel distribution building

The options above for Eclipse apply to VSCode as well.
Read above for how to use `MX_IDE_ECLIPSE_ASYNC_DISTRIBUTIONS`.

#### Mx and Suite Development

VSCode is a capable Python IDE, so just adding the `mx.*` folders to your workspace is enough.
Upon opening a Python file for the first time, VSCode will ask if it should install the recommended extensions.

##### Formatting

You can use the [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) extension.
See the [Style Guide](./Styleguide.md) for more information on custom options.
4 changes: 2 additions & 2 deletions src/mx/_impl/mx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3665,7 +3665,7 @@ def _register_visit(s):

def get_mx_path():
"""Absolute path to the mx executable"""
return join(_mx_home, 'mx')
return join(_mx_home, 'mx.cmd' if is_windows() else 'mx')


# Location of mx repo
Expand Down Expand Up @@ -18197,7 +18197,7 @@ def alarm_handler(signum, frame):
_CACHE_DIR = get_env('MX_CACHE_DIR', join(dot_mx_dir(), 'cache'))

# The version must be updated for every PR (checked in CI) and the comment should reflect the PR's issue
version = VersionSpec("7.31.0") # GR-57631 Extend mx with benchpoints command
version = VersionSpec("7.31.1") # GR-58394 Fix Eclipse config for VSCode and Windows

_mx_start_datetime = datetime.utcnow()

Expand Down
90 changes: 87 additions & 3 deletions src/mx/_impl/mx_ide_eclipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"generate_eclipse_workingsets",
]

import os, time, zipfile, tempfile
import json, os, re, time, zipfile, tempfile
# TODO use defusedexpat?
import xml.parsers.expat, xml.sax.saxutils, xml.dom.minidom
from collections import namedtuple
Expand Down Expand Up @@ -455,6 +455,86 @@ def make_eclipse_launch(suite, javaArgs, jre, name=None, deps=None):
mx.update_file(sourcesFile, '\n'.join(sources))
return mx.update_file(launchFile, launch)


@mx.command(
'mx',
'vscodeinit',
usage_msg="Generate Eclipse project configuration suitable for using with VSCode.\nThis differs from normal Eclipse config only in how annotation processors paths are set up, to account for differences in the VSCode Eclipse language server versus full Eclipse."
)
def vscodeinit(args):
"""(re)generate Eclipse project configurations and working sets for VSCode usage"""
parser = ArgumentParser(prog='mx vscodeinit')
parser.add_argument('--no-build', action='store_false', dest='buildProcessorJars', help='Do not build annotation processor jars.')
parser.add_argument('-f', '--force', action='store_true', dest='force', default=False, help='Ignore timestamps when updating files.')
args = parser.parse_args(args)

eclipseinit(None, args.buildProcessorJars, logToConsole=True, force=args.force, absolutePaths=True, pythonProjects=False)

projects = sorted([p.dir for suite in mx.suites(True) for p in suite.projects if exists(join(p.dir, ".project"))])
dists = sorted([d.get_ide_project_dir() for suite in mx.suites(True) for d in suite.dists if exists(join(getattr(d, "get_ide_project_dir", lambda: "")() or "", ".project"))])
workspace = {
"folders": [{"path": p} for p in projects + dists],
"settings": {
# Do not use shared indexes, otherwise VSCode will try to share
# index data even across different checkouts and get confused
"java.sharedIndexes.enabled": "off",
# Add Eclipse default filters minus org.graalvm.* and jdk.* so we
# get completion for JVMCI and Graal code
"java.completion.filteredTypes": [
"java.awt.*",
"com.sun.*",
"sun.*",
"io.micrometer.shaded.*"
],
},
}

# Add debug attach config for the first project
java_projects = [p for p in mx.primary_suite().projects if p.isJavaProject()]
if java_projects:
workspace["launch"] = {
"configurations": [
{
"type": "java",
"name": f"Attach to {mx.primary_suite().name}",
"projectName": java_projects[0].name,
"request": "attach",
"hostName": "localhost",
"port": 8000,
},
],
}

# Configure JDKs
if _EclipseJRESystemLibraries:
rts = workspace["settings"]["java.configuration.runtimes"] = []
for idx, name in enumerate(_EclipseJRESystemLibraries):
rts.append({
"name": name,
"path": mx.get_jdk(re.sub("[^0-9]+", "", name)).home,
"default": idx == 0
})

workspace_dir = dirname(abspath(mx.primary_suite().vc_dir))
workspace_file = join(workspace_dir, mx.primary_suite().name + ".code-workspace")
with open(workspace_file, "w") as f:
json.dump(workspace, f)

mx.log(f'''
----------------------------------------------
VSCode project generation successfully completed for {workspace_file}
The recommended next steps are:
1) Run mx build. This ensures all shaded JARs and annotation processors are built.
2) Open VSCode.
3) Make sure you have installed the 'Language Support for Java' extension.
4) Open {workspace_file} as workspace.
Note that setting MX_BUILD_EXPLODED=true can improve build times. See "Exploded builds" in the mx README.md.
----------------------------------------------
''')


@mx.command('mx', 'eclipseinit')
def eclipseinit_cli(args):
"""(re)generate Eclipse project configurations and working sets"""
Expand Down Expand Up @@ -593,7 +673,7 @@ def _add_eclipse_linked_resources(xml_doc, project_loc, linked_resources, absolu
xml_doc.open('link')
xml_doc.element('name', data=lr.name)
xml_doc.element('type', data=lr.type)
xml_doc.element('locationURI', data=get_eclipse_project_rel_locationURI(lr.location, project_loc) if not absolutePaths else lr.location)
xml_doc.element('locationURI', data=get_eclipse_project_rel_locationURI(lr.location, project_loc) if not absolutePaths else f"file://{lr.location.replace(os.sep, '/')}")
xml_doc.close('link')
xml_doc.close('linkedResources')

Expand All @@ -610,6 +690,7 @@ def _eclipse_project_rel(project_loc, path, linked_resources, res_type=IRESOURCE
return name
else:
return os.path.relpath(path, project_loc)

def _eclipseinit_project(p, files=None, libFiles=None, absolutePaths=False):
# PROJECT_LOC Eclipse variable
project_loc = mx_util.ensure_dir_exists(p.dir)
Expand Down Expand Up @@ -853,7 +934,10 @@ def processDep(dep, edge):
processorsPath = mx.classpath_entries(names=processors)
for e in processorsPath:
if e.isDistribution() and not isinstance(e.suite, mx.BinarySuite):
out.element('factorypathentry', {'kind' : 'WKSPJAR', 'id' : f'/{e.name}/{basename(e.path)}', 'enabled' : 'true', 'runInBatchMode' : 'false'})
if absolutePaths:
out.element('factorypathentry', {'kind' : 'EXTJAR', 'id' : e.path, 'enabled' : 'true', 'runInBatchMode' : 'false'})
else:
out.element('factorypathentry', {'kind' : 'WKSPJAR', 'id' : f'/{e.name}/{basename(e.path)}', 'enabled' : 'true', 'runInBatchMode' : 'false'})
elif e.isJdkLibrary() or e.isJreLibrary():
path = e.classpath_repr(jdk, resolve=True)
if path:
Expand Down

0 comments on commit a090938

Please sign in to comment.