godotengine / godot-csharp-vscode

Debugger and utilities for working with Godot C# projects in VSCode
https://marketplace.visualstudio.com/items?itemName=neikeq.godot-csharp-vscode
MIT License
144 stars 21 forks source link

Add a 'clean' task to C# task list #45

Open blackears opened 2 years ago

blackears commented 2 years ago

I am sometimes encountering odd bugs where my code is behaving as if other code I have written does not exist or has not been edited. When I encounter similar situations in a C++ environment, it usually means my object files have become out of sync with my source and usually running a 'make clean' will fix it. It would be very helpful to have a similar feature in the C# tasks so that you can make sure that your bugs are actually there and not the result of out of date assemblies.

tvardero commented 1 year ago

Encountered the same issue with Godot 4. Looks like when launching game from Godot editor, the latter does not recompile source code. If you are in Godot 4, run dotnet build in your game project folder before each launch. dotnet clean is not necessary.

raulsntos commented 1 year ago

The Godot editor and the extension's build task should work and there should be no need for a clean task. A clean task would also prevent incremental builds.

If a project launched with an outdated assembly it likely failed to compile, if the build task failed VSCode should show a dialog complaining about it but I think our current build task that uses godot --build-solutions may not exit with a non-zero exit code so VSCode may not know it failed.

Using the Play in Editor debug configuration should trigger a build in Godot's editor since it always builds the project before launching it. Using the Launch debug configuration will not automatically build the project, that's why the build task exists and the extension generates it and enables it as a pre-launch task so the project is built when using this debug configuration as well, if your configuration does not have preLaunchTask: 'build' it may have been generated before this was added to the extension, try generating the configuration again.

Keep in mind that Godot 4.0 is not supported by this extension, so it won't work regardless of what I just said.

blackears commented 1 year ago

Why would a clean task prevent incremental builds? You don't need to run it every single time. Just provide it as an option to help fix things which you can run when the source and the binaries get out of sync for whatever reason.

raulsntos commented 1 year ago

Yes I meant running it on every build would prevent incremental builds. I feel like this is unrelated to Godot and the C# extension would be a better fit to provide this task. I'm still not convinced that this task is necessary though, binaries should not get out of sync and I'd be more interested in investigating the real cause of the issue than providing a hacky patch.

But if you still want a clean task it's very easy to create your own. This should do it:

{
    "label": "clean",
    "command": "dotnet",
    "type": "process",
    "args": [
        "clean",
        "${workspaceFolder}"
    ],
    "problemMatcher": "$msCompile"
}
blackears commented 1 year ago

Do you know how to invoke the clean task from within Visual Studio Code? I can't find a mechanism for it.

tvardero commented 1 year ago

Do you know how to invoke the clean task from within Visual Studio Code? I can't find a mechanism for it.

@blackears

Your .vscode/tasks.json file should look like this (those two tasks are like an example):

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "label": "clean",
            "command": "dotnet",
            "type": "process",
            "args": [
                "clean"
            ],
            "problemMatcher": []
        }
    ]
}

To invoke those, you can either:

  1. Add preLaunchTask": "YOUR_TASK" line to your launch.json configuration.
  2. Call them directly via command palette (Ctrl + Shift + P), then >Tasks: Run Task and select your desired task.
  3. Set one of tasks as default build task, so you can call it with keybind Ctrl + Shift + B (Run build task).
  4. Bind keyboard shortcut to your task.

You can also chain tasks to run in sequence or in parallel. Read this. (Hint: you need to use dependsOn and dependsOrder fields in your tasks. For example, "build" tasks depends on "clean" tasks to run first.)


Please note, that dotnet clean is not necessary. To ensure your source code is "fresh and clean" all you need to call is dotnet build only.

tvardero commented 1 year ago

Off-topic

For those who are curious about my tasks.json and launch.json configurations:

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile",
        }
    ]
}

launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Select and Launch Scene",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
// Below is absolute path to your Godot.exe. Or use PATH env. var. to shorten it to something like "godot4"
            "program": "D:\\\\Programs\\\\Utils\\\\Godot4.0b2\\\\godot4.exe",
            "args": [
                "${input:pickScene}"
            ],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": "Launch Main Scene",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "D:\\\\Programs\\\\Utils\\\\Godot4.0b2\\\\godot4.exe", 
            "args": [
                "--path",
                "${workspaceFolder}"
            ],
            "cwd": "${workspaceFolder}",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": "Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ],
    "inputs": [
        {
            "id": "pickScene",
            "type": "command",
            "command": "filePicker.pick",
            "args": {
                "masks": "**/*.tscn",
                "display": "filePath",
                "output": "filePath"
            }
        }
    ]
}

NOTE: you need this extension to use launch configuration "Select and Launch Scene".


Actually, Godot 4 makes this (godot-csharp-vscode) extension obsolete. (Sorry, I didn't want to hurt anyone here 👉👈 ). You can debug, use breakpoints, etc. just using C# extension by Microsoft / Omnisharp , which is a must-have for all C# developers in VS Code.

There is, actually, a slight problem (will reference in separate issue later on godot repo) with Intellisense and Godot's source generation. Sometimes you will be redirected to non-existed files (with path somewhere in your project's .godot folder) when going for definitions of Godot classes.