Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀 Feature: ability to debug the running package code #1145

Closed
3 tasks done
johnnyreilly opened this issue Dec 30, 2023 · 7 comments · Fixed by #1153
Closed
3 tasks done

🚀 Feature: ability to debug the running package code #1145

johnnyreilly opened this issue Dec 30, 2023 · 7 comments · Fixed by #1153
Labels
status: accepting prs Please, send a pull request to resolve this! type: feature New enhancement or request

Comments

@johnnyreilly
Copy link
Collaborator

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Overview

I love being able to debug. Whenever I'm making a package I try to set up a way to debug the code in VS Code. Either using a combo of tsc / sourcemaps and Node.js or using ts-node.

I think it'd be awesome if CTA had this built in. I'd be potentially up for contributing this. My instinct would be to prefer using just tsc / sourcemaps and Node.js to avoid adding another dependency.

What do you think?

Additional Info

N/A

@johnnyreilly johnnyreilly added the type: feature New enhancement or request label Dec 30, 2023
@JoshuaKGoldberg JoshuaKGoldberg added the status: accepting prs Please, send a pull request to resolve this! label Dec 30, 2023
@johnnyreilly
Copy link
Collaborator Author

johnnyreilly commented Dec 30, 2023

Got a rough version working locally in VS Code. Implementation looks like this:

launch.json file:

{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"type": "node",
			"request": "launch",
			"name": "Launch Program",
			"skipFiles": ["<node_internals>/**"],
			"program": "debug/index.js",
			"preLaunchTask": "build:debug"
		}
	]
}

task.json file:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "npm",
			"script": "debug",
			"problemMatcher": [],
			"label": "build:debug",
			"detail": "Build a debug version of the project"
		}
	]
}

The scripts of package.json has a new entry:

		"debug": "rimraf debug && tsc --noEmit false --outDir debug",

And requires a dev dependency of rimraf to delete the debug folder prior to debug builds.

With the above in place, you can F5 in VS Code and debug, hitting your breakpoints. Is this an implementation that would work well generally?

@JoshuaKGoldberg
Copy link
Owner

Hmmm. Hmm... I' really prefer not to add to package.json or take on new directories for builds. Is there a way to reuse the existing tsup builder (pnpm build) instead?

@johnnyreilly
Copy link
Collaborator Author

johnnyreilly commented Dec 30, 2023

Yeah I think so - given that the tsup config has 2 key settings set we're actually in a decent position:

import { defineConfig } from "tsup";

export default defineConfig({
	bundle: false,
	clean: true, // WILL CLEAN DIRECTORY PRIOR TO BUILD MEANING WE DON'T DEBUG STALE CODE
	dts: true,
	entry: ["src/**/*.ts"],
	format: "esm",
	outDir: "lib",
	sourcemap: true, // WILL ALLOW US TO DEBUG ORIGINAL SOURCE CODE
});

Given these are in place I think we might only need tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "npm",
			"script": "build",
			"problemMatcher": [],
			"label": "build",
			"detail": "Build the project"
		}
	]
}

and launch.json:

{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"type": "node",
			"request": "launch",
			"name": "Debug Program",
			"skipFiles": ["<node_internals>/**"],
			"program": "lib/index.js",
			"preLaunchTask": "build"
		}
	]
}

@JoshuaKGoldberg
Copy link
Owner

Awesome. Let's do it!

@johnnyreilly
Copy link
Collaborator Author

I'll put a PR together! Out of curiosity, what is the motivation for using tsup? I've read the docs and this:

https://jakeginnivan.medium.com/options-for-publishing-typescript-libraries-9c37bec28fe

But I'm not super clear on the benefits of using it over tsc. Is it about speed of compilation?

@JoshuaKGoldberg
Copy link
Owner

It's a few things:

Being able to do all that in a single tool run by a single pnpm run build is nice and straightforward. I like that!

JoshuaKGoldberg pushed a commit that referenced this issue Jan 9, 2024
<!-- 👋 Hi, thanks for sending a PR to create-typescript-app! 💖.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

- [x] Addresses an existing open issue: fixes #1145
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

<!-- Description of what is changed and how the code change does that.
-->

This PR enables debugging by adding a `tasks.json`

```json
{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "npm",
			"script": "build",
			"problemMatcher": [],
			"label": "build",
			"detail": "Build the project"
		}
	]
}
```

and the below configuration to the `launch.json`:

```json
{
	// Use IntelliSense to learn about possible attributes.
	// Hover to view descriptions of existing attributes.
	// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
	"version": "0.2.0",
	"configurations": [
		{
			"type": "node",
			"request": "launch",
			"name": "Debug Program",
			"skipFiles": ["<node_internals>/**"],
			"program": "lib/index.js",
			"preLaunchTask": "build"
		}
	]
}
```

Here's an example of debugging using it:

![screenshot of VS Code
debugging](https://github.com/JoshuaKGoldberg/create-typescript-app/assets/1010525/09046b0e-16cc-4a3c-90ab-c81de044e17d)
Copy link

🎉 This is included in version v1.54.0 🎉

The release is available on:

Cheers! 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Please, send a pull request to resolve this! type: feature New enhancement or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants