luau icon indicating copy to clipboard operation
luau copied to clipboard

[New Solver] Type Error with pcall Return Values in Luau Type Checker Beta (errorMessage Not Recognized)

Open BleckWolf25 opened this issue 6 months ago • 2 comments

When using the Luau type checker beta feature with --!strict mode, a type error occurs in my module MCS_Remotes.luau where the second return value (errorMessage) from pcall is not recognized, despite pcall always returning two values (boolean, any). This prevents proper error handling in the init function of the Modular Command System (MCS) middleware.

Error Message:

Type Error: (246,26) Function only returns 1 value, but 2 are required here

Code Context: The error occurs in the init function at the pcall invocation:

-- MCS_Remotes.luau, line 237-303
-- Initialize the remotes system
local function init(): boolean
	if isInitialized then
		Utils.print(MODULE_NAME, "Remotes system already initialized")
		return true
	end

	Utils.startTimer(MODULE_NAME, "Init")

	local success, errorMessage: any = pcall(function(): ()
		-- Create or get MCS folder
		local mcsFolder = ReplicatedStorage:FindFirstChild("MCS")
		if not mcsFolder then
			mcsFolder = Instance.new("Folder")
			mcsFolder.Name = "MCS"
			mcsFolder.Parent = ReplicatedStorage
		end
		assert(mcsFolder:IsA("Folder"), "MCS folder is not a Folder instance")

		-- Create or get Remotes folder
		remoteFolder = mcsFolder:FindFirstChild("Remotes")
		if not remoteFolder then
			remoteFolder = Instance.new("Folder")
			remoteFolder.Name = "Remotes"
			remoteFolder.Parent = mcsFolder
		end
		assert(remoteFolder:IsA("Folder"), "Remotes folder is not a Folder instance")

		-- Create command remote
		commandRemote = Instance.new("RemoteEvent")
		commandRemote.Name = "CommandRemote"
		commandRemote.Parent = remoteFolder

		-- Create autocomplete remote
		autocompleteRemote = Instance.new("RemoteFunction")
		autocompleteRemote.Name = "AutocompleteRemote"
		autocompleteRemote.Parent = remoteFolder

		-- Connect command remote
		commandRemote.OnServerEvent:Connect(function(player: Player, request: Types.CommandRequest)
			local response = handleCommandRequest(player, request)
			if commandRemote then
				commandRemote:FireClient(player, response)
			else
				Utils.print(MODULE_NAME, "Warning: commandRemote is nil during FireClient")
			end
		end)

		-- Connect autocomplete remote
		autocompleteRemote.OnServerInvoke = function(player: Player, input: string): Types.QueryResponse
			return handleAutocompleteRequest(player, input)
		end

		isInitialized = true
		Utils.print(MODULE_NAME, "Remotes system initialized successfully")
	end)
	
	Utils.endTimer(MODULE_NAME, "Init")

	if not success then
		Utils.print(MODULE_NAME, string.format("Failed to initialize remotes: %s", tostring(errorMessage or "Unknown error")))
		return false
	end

	return true
end

Expected Behavior:

  • pcall should return two values: a boolean (success) and an any (errorMessage).
  • The type checker should allow destructuring both values without error.
  • The errorMessage should be usable in subsequent error handling (tostring(errorMessage or "Unknown error")).

Actual Behavior:

  • The Luau type checker beta incorrectly infers that pcall returns only one value, causing a type error when assigning to success and errorMessage.
  • This blocks compilation and prevents proper error handling.

How to Reproduce:

  1. Create a Roblox Luau script (any_name.luau) with the above pcall code in --!strict mode.
  2. Enable the Luau type checker beta feature in Roblox Studio.
  3. Open the module.
  4. Observe the error at the pcall line: Type Error: (246,26) Function only returns 1 value, but 2 are required here.

Environment:

  • Luau Version: 0.678
  • Roblox Studio Version: Latest stable release (Jun 17, 2025)
  • Trype Checking: --!strict
  • OS: MacOS
  • Project Context: Modular Command System (MCS) centralized remotes that creates and handles all events within MCS on roblox, using ReplicatedStorage.MCS.Remotes for client-server communication

Attempted Fixes:

  • Explicitly typed success: boolean, errorMessage: any (no effect).
  • Explicitly typed success: boolean, errorMessage: string (no effect).
  • Removed success type annotation, keeping errorMessage: any (no effect).
  • Removed errorMessage type annotation, keeping success: any (no effect).
  • Added function return type : () to clarify the pcall function’s signature (no effect).
  • Tried to fix this with AI (no effect).

Notes:

  • The error does not occur without --!strict AND/or beta luau type checker.
  • The pcall is used to handle potential errors during folder and remote creation ( Instance.new, assert, etc.).
  • Related modules (MCS_Configuration, MCS_Types, MCS_Utils) do not appear to affect this issue, but I can provide them if needed.
  • This may be a bug in the Luau type checker beta’s handling of pcall return types in strict mode.
  • This is not the first time I see this type warning.

BleckWolf25 avatar Jun 17 '25 19:06 BleckWolf25

Bug still not fixed.

BleckWolf25 avatar Jul 06 '25 17:07 BleckWolf25

I'm also encountering this error, here's a more concise repro. Interestingly, there's no type error if the solver can infer in compile time that the function does in fact either error or return a value.

local _success, _result = pcall(function() -- TypeError: Function only returns 1 value, but 2 are required here
	if true then
		error("hello")
	end
end)
local _success2, _result2 = pcall(function() -- ✓
	error("hello")
end)
local _success3, _result3 = pcall(function() : number -- ✓
	return 5
end)

Waffle3z avatar Sep 03 '25 21:09 Waffle3z

still not fixed

monskov avatar Dec 07 '25 23:12 monskov